我想使用htaccess将每个旧URL重定向到相应的新重写URL

时间:2015-10-25 08:47:03

标签: php apache .htaccess redirect

经过大量研究后,我得出的结论是,我被困住了,需要帮助。这是我的htaccess:

ErrorDocument 404 http://website.com/404.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule . http://%1%{REQUEST_URI} [R=301,L]
RewriteRule contact contact.php [NC,L]
RewriteRule about display.php?id=2 [NC,L]
RewriteRule services display.php?id=4 [NC,L]
RewriteRule rss.xml?$ rss.php [L]
</IfModule>

这为每个display.php?id=查询成功创建了一个重写的URL。现在我有两个网址,如何将display.php?id=2永久重定向到about

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

首先,清除浏览器缓存。我们不希望任何缓存的永久重定向,你已经尝试搞乱我们的新逻辑:

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
#Use 302 redirects for debugging (the default if no redirect code is provided):
RewriteRule ^/?contact/?$ contact.php [NC,L,R]
RewriteRule ^/?about/?$ display.php?id=2 [NC,L,R]
RewriteRule ^/?services/?$ display.php?id=4 [NC,L,R]
RewriteRule ^/?rss.xml/?$ rss.php [NC,L,R]

如果您使用的是谷歌浏览器,可以按ctrl + shift + j打开开发人员工具。然后单击“网络”选项卡并选中“禁用缓存”复选框。然后再次尝试您的请求,看看它是否正常工作。请注意我的代码使用302重定向,这是调试的最佳实践。一旦您可以验证它是否按预期工作,您就可以将302重定向更改为301重定向以进行生产。

修改

要反过来重定向所有内容,请使用此代码:

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

#Use 302 redirects for debugging (the default if no redirect code is provided):
RewriteRule ^/?contact.php/?$ contact [NC,L,R]
RewriteCond %{QUERY_STRING} (^|&)id=2(&|$)
RewriteRule ^/?display\.php/?$ about? [NC,L,R]
RewriteCond %{QUERY_STRING} (^|&)id=4(&|$)
RewriteRule ^/?display\.php/?$ services? [NC,L,R]
RewriteRule ^/?rss.php/?$ rss.xml [NC,L,R]

<强> EDIT2:

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

#handle silent rewrites first:
RewriteCond %{THE_REQUEST} ^\w+\s+/?about
RewriteRule ^/?about/?$ /display.php?id=2&r=%1 [NC,L]

RewriteCond %{THE_REQUEST} ^\w+\s+/?contact
RewriteRule ^/?contact/?$ contact.php [NC,L]

RewriteCond %{THE_REQUEST} ^\w+\s+/?services
RewriteRule ^/?services/?$ display.php?id=4 [NC,L]

RewriteCond %{THE_REQUEST} ^\w+\s+/?rss\.xml
RewriteRule ^/?rss.xml/?$ rss.php [NC,L]


#handle redirects:
RewriteCond %{THE_REQUEST} ^\w+\s+/?display.php
RewriteCond %{QUERY_STRING} (^|&)id=2(&|$)
RewriteRule ^/?display\.php/?$ about? [NC,L,R]

RewriteCond %{THE_REQUEST} ^\w+\s+/?contact\.php
RewriteRule ^/?contact.php/?$ contact [NC,L,R]

RewriteCond %{THE_REQUEST} ^\w+\s+/?display\.php
RewriteCond %{QUERY_STRING} (^|&)id=4(&|$)
RewriteRule ^/?display\.php/?$ services? [NC,L,R]

RewriteCond %{THE_REQUEST} ^\w+\s+/?rss\.php
RewriteRule ^/?rss.php/?$ rss.xml [NC,L,R]

<强> EDIT3: 实际上,我们可以在这里使用END标志来获得更清晰的代码:

#remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

#handle silent rewrites first:
RewriteRule ^/?about/?$ /display.php?id=2&r=%1 [NC,L,NS,END]
RewriteRule ^/?contact/?$ contact.php [NC,L,NS,END]
RewriteRule ^/?services/?$ display.php?id=4 [NC,L,NS,END]
RewriteRule ^/?rss.xml/?$ rss.php [NC,L,NS,END]

#handle redirects:
RewriteCond %{QUERY_STRING} (^|&)id=2(&|$)
RewriteRule ^/?display\.php/?$ about? [NC,L,R,END]

RewriteRule ^/?contact.php/?$ contact [NC,L,R,END]

RewriteCond %{QUERY_STRING} (^|&)id=4(&|$)
RewriteRule ^/?display\.php/?$ services? [NC,L,R,END]

RewriteRule ^/?rss.php/?$ rss.xml [NC,L,R,END]