Simple website structure RewriteRule for PHP GET parameters

萝らか妹 提交于 2019-12-13 02:03:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!