【发布时间】:2016-08-19 22:02:15
【问题描述】:
我的页面中有 2 种类型的 url。
1-http://www.example.com/index.php?id=12
我一直想要这个:
但对于其他类似的东西:
2-http://www.example.com/index.php?id=12&id2=123&asd=qwe&svd=sdf...
我想要它没有改变:
http://www.example.com/?id=12&id2=123&asd=qwe&svd=sdf...
所以我只想重定向http://www.example.com/index.php?id=12
我试试这个
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
它只适用于第一个 (http://www.example.com/index.php?id=12)
但第二次,它重定向到 (http://www.example.com/***)
我在stackoveflow中阅读我必须使用[QSA,L]
所以我试试这个:
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [QSA,L]
但我收到 500 错误(内部服务器错误)
这是我的完整代码:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
---编辑---
我可以这样做
RewriteRule ^index.php/([^/]+)/?([^/]*) /index.php?id=$1 [NC]
例如 www.example.com/123 有效!
但 www.example.com/index.php?id=123 不会重定向到 www.example.com/12
【问题讨论】:
标签: .htaccess url mod-rewrite url-rewriting url-redirection