I need some help with rewriting different URL's for my website. It is a multilanguage site so that complicates it a bit. I have managed to get some things work. This is what my code does so far:
But I have two problems with the code:
Besides that, I would like to have two additional rewriterules but I don't want my htaccess to be a mess. So I hope someone can point me in the right direction. What is the best way to do this in my htaccess?
The parameter 'page' contains the page that is displaying and should be rewrited: http://www.example.com/en/index.php?page=info -> http://www.example.com/en/info/ (I already figured out how to hide the index.php, see code below)
My News page ( http://www.example.com/en/index.php?page=news&id=2452&news=lorem-ipsum ) should be rewrited to http://www.example.com/en/news/2452/lorem-ipsum/ .
And for all of these rules: the URL should end with /, if it doesn't, it should add it: so http://www.example.com/en/news -> http://www.example.com/en/news/ .
This is my code now:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# SET REWRITEBASE VARIABLE
RewriteCond %{HTTP_HOST} !=localhost
RewriteRule ^ - [E=FRB:/]
RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^ - [E=FRB:/www.example.com/]
## REWRITE FOR LANGUAGE ##
# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ %{ENV:FRB}en/ [R=301,L]
# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|nl)$ %{ENV:FRB}$1/ [R=301,L]
# now all urls have en/ de/ -> parse them
RewriteRule ^(en|nl)/(.*)$ %{ENV:FRB}$2?lang=$1&%{query_STRING} [L]
## REWRITE NON-WWW TO WWW (EXCEPT ON LOCALHOST) ##
RewriteEngine On
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !^(.*)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
## REMOVE INDEX.PHP FROM URL ##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Thank you very much for helping me out! If you have questions, please let me know.
Have it like this:
Options +FollowSymlinks
RewriteEngine On
# SET REWRITEBASE VARIABLE
RewriteCond %{HTTP_HOST} !=localhost
RewriteRule ^ - [E=FRB:/]
RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^ - [E=FRB:/www.example.com/]
## REWRITE FOR LANGUAGE ##
# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ %{ENV:FRB}en/ [R=301,L]
## REMOVE INDEX.PHP FROM URL ##
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L,NE]
## REWRITE NON-WWW TO WWW (EXCEPT ON LOCALHOST) ##
RewriteCond %{HTTP_HOST} !(^www\.|localhost) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?[^/])$ %{REQUEST_URI}/ [L,R=302,NE]
# handle /en/news/2452/lorem-ipsum/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*[a-z]{2})/?\?page=([^\s&]+)&id=([^\s&]+)&news=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/? [R=302,L,NE]
RewriteRule ^(en|nl)/(news)/(\d+)/([\w-]+)/?$ index.php?lang=$1&page=$2&id=$3&news=$4 [L,QSA]
# handle /en/info
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*[a-z]{2})/?\?page=([^\s&]+) [NC]
RewriteRule ^ %1/%2/? [R=302,L,NE]
RewriteRule ^(en|nl)/([\w-]+)/?$ index.php?lang=$1&page=$2 [L,QSA]
# now all urls have en/ OR de/ -> parse them
RewriteRule ^(en|nl)/(.*)$ %{ENV:FRB}$2?lang=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?$1 [L,QSA]
Try removing the R=301
on the RewriteRule.
With that command you're telling the server it should answer with a 301 error code and gives the new url to the browser. The browser will take that URL and will do a new GET.
Without the R=301 the server will do the "magic" internally ;-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.