.htaccess 使用重写规则重定向 301
我正在尝试美化一些网址。我配置了 htaccess 文件,以便更改我的网址:
旧网址: http://mysite.com/index .php?id=45tye4 新网址:http://mysite.com/45tye4
我现在想将旧网址永久重定向到新网址。这是我尝试的但没有运气:
RewriteRule ^index.php?id=(.*)$ $1 [R=301,L]
主要问题似乎是“?”在网址中。当我尝试不带 ? 的相同网址时重定向有效。我还尝试了其他变体,但没有成功:
RewriteRule ^index.php\?id=(.*)$ $1 [R=301,L]
RewriteRule ^index.php[\?]id=(.*)$ $1 [R=301,L]
更新:
我根据 anubhava 说明添加了重定向。重定向有效,但不幸的是我陷入了重定向循环。我认为 [L] 标志应该解决重定向循环,但事实并非如此。
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^index\.php/?$ /%1? [R=301,L]
RewriteRule ^(.*)$ index.php?id=$1 [L]
I'm trying to beautify some urls. I configured the htaccess file so my urls are changed:
old url: http://mysite.com/index.php?id=45tye4
new url: http://mysite.com/45tye4
I want now to permanently redirect old urls to new urls. This is what I try with no luck:
RewriteRule ^index.php?id=(.*)$ $1 [R=301,L]
The main problem seems to be the '?' in the url. When I try the same url without ? the redirect works. I also tried other variants with no luck:
RewriteRule ^index.php\?id=(.*)$ $1 [R=301,L]
RewriteRule ^index.php[\?]id=(.*)$ $1 [R=301,L]
Update:
I added the redirection according to anubhava instructions. The redirection works, but unfortunately I get into a redirect loop. I thought [L] flag should solve the redirection loop, but it doesn't.
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^index\.php/?$ /%1? [R=301,L]
RewriteRule ^(.*)$ index.php?id=$1 [L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RewriteRule 仅匹配 REQUEST_URI。您必须使用 RewriteCond 来匹配查询字符串
尝试以下代码:
这会将
/index.php?id=45tye4
的旧 URI 重定向到新 URI:/45tye4
RewriteRule matches only REQUEST_URI. You have to use RewriteCond to match Query string
Try this code:
This will redirect old URI of
/index.php?id=45tye4
to new URI:/45tye4