htaccess redirect for dynamic urls not working

孤人 提交于 2019-12-13 05:29:13

问题


I basically want:

http://example.com/index.php?page=abc to redirect to http://www.exmaple.com/abc

I have:

RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^index.php$ %1? [R=301]
RewriteRule ^%1$ index.php?page=%1 [L]

回答1:


Some syntax issues in your 2nd rule and logic ones in your 1st rule.

Following should work:

RewriteEngine On

# for external redirection from `/index.php?page=abc` to `/abc`
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

# for internal redirection from `/abc` to `/index.php?page=abc`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]

PS: I suggest you to read: Apache mod_rewrite Introduction



来源:https://stackoverflow.com/questions/19113994/htaccess-redirect-for-dynamic-urls-not-working

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