问题
How can I rewrite the examples below?
www.domain-name.com/site-name/index.php?lang=en-us&page=home
www.domain-name.com/site-name/index.php?lang=en-us&page=about
www.domain-name.com/site-name/index.php?lang=pt-pt&page=inicio
www.domain-name.com/site-name/index.php?lang=pt-pt&page=sobre
To this:
www.domain-name.com/site-name/en-us/home.html
www.domain-name.com/site-name/en-us/about.html
www.domain-name.com/site-name/pt-pt/inicio.html
www.domain-name.com/site-name/pt-pt/sobre.html
I managed to get the page parameter. This is what I have:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /site-name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?page=$1 [L]
</IfModule>
I don't know how to append .html in the end for all "page" parameters and regex are really not my thing, so Im having some hard time figuring out how the "-" part for the "lang" parameter will work. I've noticed that website gets weird when incorrect url's are typed.
Later it will have it's own domain name, but I guess changing from "RewriteBase /site-name/" to "RewriteBase /" will fix it, right?
I don't know much about it, but I think some flags are missing too (for SEO purposes).
回答1:
You can use:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /site-name/
RewriteRule ^([^/]+)/(.+?)\.html?$ index.php?lang=$1&page=$2 [NC,L,QSA]
</IfModule>
来源:https://stackoverflow.com/questions/27496757/simple-website-structure-rewriterule-for-php-get-parameters