htaccess redirect dynamic urls

南笙酒味 提交于 2019-12-24 16:56:46

问题


I am trying to write a htaccess redirect, but it is not working as I want it to. My current (working) htaccess redirects all html pages with 1 character to the index file as:

RewriteRule ^([a-zA-Z0-9]).html$ index.php?letter=$1

So a.html gets redirected to index.php?letter=a Now I need to redirect the page a.html?page=2 to index.php?letter=a&page=2 Basically I want to redirect the url, but leave the dynamic part intact. All of the following return a 404:

RewriteRule ^([a-zA-Z0-9]).html?page=([0-9]+) index.php?letter=$1&page=$2

RewriteRule ^([a-zA-Z0-9]).html?page=(.*) index.php?letter=$1&page=$2

RewriteCond %{QUERY_STRING} page=(.*)
RewriteRule ^([a-zA-Z0-9]).html(.*) index.php?letter=$1&page=%1

I think I'm close, but I can't seem to get there :/ could anyone give me the last push?


回答1:


Your RewriteRule needs to be

RewriteRule ^([a-zA-Z0-9])\.html$ index.php?letter=$1 [QSA,NC,L]

Please, note that URL parameters are not available for matching within the RewriteRule. If you simply need to append an extra URL parameter you can do so along with the [QSA] flag which would take care of appending the original URL parameters for you.

Please, note that the dot before html needs to be escaped \. as well. The [L] makes sure that rewriting stops and no further rules (if any below) are applied.



来源:https://stackoverflow.com/questions/19224396/htaccess-redirect-dynamic-urls

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