i've got .htaccess file:
RewriteEngine on
RewriteRule ^category?$ category.php
RewriteRule ^category/([0-9a-zA-Z]+) category.php?name=$1
Need to even use letters "ě,š,č,ř,ž,ý,á,í,é" etc..
How to do that?
You can use:
RewriteEngine on
RewriteRule ^category?$ category.php [NC,L]
RewriteRule ^category/([^/]+)$ category.php?name=$1 [NC,L]
Which accepts all characters
Related
I have a working RewriteRule that for example http://mydomain/stack rewrites to decoder.php?decode=stack and then redirects it to the correct website because decoder.php looks up in its database what the redirect should be.
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
RewriteRule ^phpmyadmin$ phpmyadmin [L]
RewriteRule ^([\w\d-]{1,})$ decoder.php?decode=$1 [L]
But now I need "more levels". First I tried with a dot. Eg. http://mydomain/l1.stack, but I couldn't get my RewriteRule to work. Anyway: the better approach would be http://mydomain/l1/stack to rewrite to decoder.php?level=l1&decode=stack or http://mydomain/a1/wH4tever. to decoder.php?level=a1&decode=wH4tever.. The "levels" that I want should be fixed in the .htaccess, but the "decode" could be any string.
One problem: I don't get any RewriteRule to work with what I want :-(.
Here a simple rule for a single, fixed and literal "level" string "l1":
RewriteEngine on
RewriteRule ^l1/([\w\d-]{1,})$ decoder.php?level=$1&decode=$2 [L]
RewriteRule ^([\w\d-]{1,})$ decoder.php?decode=$1 [L]
This should do if there are multiple fixed literal "level" strings (here "l1" or "a1")
RewriteEngine on
RewriteRule ^(l1|a1)/([\w\d-]{1,})$ decoder.php?level=$1&decode=$2 [L]
RewriteRule ^([\w\d-]{1,})$ decoder.php?decode=$1 [L]
That would be the generic variant:
RewriteEngine on
RewriteRule ^([\w\d-]{1,})/([\w\d-]{1,})$ decoder.php?level=$1&decode=$2 [L]
RewriteRule ^([\w\d-]{1,})$ decoder.php?decode=$1 [L]
Test yourself: htaccess.madewithlove.com
Lets think that I have such code in htaccess:
RewriteRule ^admin/registration/?$ admin/qeyd.php [NC,L]
RewriteRule ^admin/login/?$ admin/login.php [NC,L]
RewriteRule ^admin/profile/?$ admin/profile.php [NC,L]
But I want to define a variable for "admin" and write it like this:
admin_directory_variable = custom_name
RewriteRule ^custom_name/registration/?$ admin/qeyd.php [NC,L]
RewriteRule ^custom_name/login/?$ admin/login.php [NC,L]
RewriteRule ^custom_name/profile/?$ admin/profile.php [NC,L]
How can I do it ?
Create admin/.htaccess file and use these rules:
RewriteEngine On
RewriteBase /admin/
RewriteRule ^registration/?$ qeyd.php [NC,L]
RewriteRule ^(login|profile)/?$ $1.php [NC,L]
Apache 2.4 has the Define keyword which you use like this:
Define name value
and use like this:
DocumentRoot ${name}
Unfortunately, it can't be used in .htaccess files. See Apache 2.4 documentation
I need to change the following url
http://somedomain.com/news/a_sample_news_article.html
to
http://somedomain.com/post/a-sample-news-article
I have this in my htaccess which works, but I am sure it can be improved upon - does anyone have a better solution?
RewriteEngine on
# replace underscores
RewriteRule ^(news)/([^_]*)_+(.*)$ /$1/$2-$3 [L,NC,R=302]
# redirect the directory from news to post
RewriteRule ^news/(.*)$ /post/$1 [R,L]
# remove .html from end of url
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
Any help much appreciated!
To redirect
/news/foo_bar
to
/post/foo-bar
you can use the following rule :
RewriteEngine on
# redirect "/news/foo_bar" to "/foo_bar"
RewriteRule ^news/(.+)$ /$1 [L,R]
#2 replace underscore with hypens
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes]
RewriteCond %{ENV:uscores} yes
RewriteRule ^(.+)$ /post/$1 [L,R]
Basically I have my website set up the following way:
mysite/site -Goes to the main index.php file
mysite/site/asdf -Goes to the main index.php file with a subpage
mysite/site/admin -Goes to an admin panel with various subpages
But the issue I'm running into is when I go to mysite/site/admin without the trailing slash, it appends /?admin=1 to the URL. Same if I go to mysite/site/admin/pages without the trailing slash, it appends /?admin=1&page=pages to the URL. I want it to not append these query strings.
If I add the trailing slash, the query string does not get appended. If I go to mysite/site/admin/pages/edit without the trailing slash, it doesn't append the query string, so it seems just the first 2 levels do this.
This is confusing and I don't understand it at all. I've tried lots of various things from googling and searching this site but nothing has worked. I'm a newbie to this .htaccess stuff. Here's what my .htaccess file looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?admin/users/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=users&subpage=view&user=$1 [L]
RewriteRule ^/?admin/users/create/?$ admin/index.php?admin=1&page=users&subpage=create [L]
RewriteRule ^/?admin/([\-_A-Za-z0-9]+)/([\-_A-Za-z0-9]+)/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2&id=$3 [L]
RewriteRule ^/?admin/([\-_A-Za-z0-9]+)/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2 [L]
RewriteRule ^/?admin/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=$1 [L]
RewriteRule ^/?admin/?$ admin/index.php?admin=1 [L]
RewriteRule ^/?([\-_A-Za-z0-9]+)/?$ index.php?page=$1 [L]
</IfModule>
Options -Indexes
Can anybody tell me what I'm doing wrong or how to fix this?
That is because /site/admin/pages is a valid directory and without trailing slash Apache's mod_dir module redirects the URL to one with a trailing slash.
To fix you can use:
RewriteEngine On
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteRule ^/?admin/users/([-\w]+)/?$ admin/index.php?admin=1&page=users&subpage=view&user=$1 [L]
RewriteRule ^/?admin/users/create/?$ admin/index.php?admin=1&page=users&subpage=create [L]
RewriteRule ^/?admin/([-\w]+)/([-\w]+)/([-\w]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2&id=$3 [L]
RewriteRule ^/?admin/([-\w]+)/([-\w]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2 [L]
RewriteRule ^/?admin/([-\w]+)/?$ admin/index.php?admin=1&page=$1 [L]
RewriteRule ^/?admin/?$ admin/index.php?admin=1 [L]
RewriteRule ^/?([-\w]+)/?$ index.php?page=$1 [L]
I have an URL like these:
http://domain.com/database/movie/jurassic-park.1224.html
http://domain.com/artist/bruno-mars.104.html
I tried to resolve these URLs with the following RewriteRules, but they don't work.
RewriteEngine On
RewriteRule ^(.*)\/movie\/(.*)\.(.*)\.html$ ./index.php?area=movies&id=$2 [QSA]
RewriteRule ^(.*)\/artist\/(.*)\.(.*)\.html$ ./index.php?area=people&id=$2 [QSA]
Who can help me what's wrong?
Try these rules:
RewriteEngine On
RewriteRule movie/(.+?)\.(\d+)\.html$ index.php?area=movies&id=$2 [L,QSA,NC]
RewriteRule artist/(.+?)\.(\d+)\.html$ index.php?area=people&id=$2 [L,QSA,NC]