首先,清除浏览器缓存。我们不希望任何您已经尝试与我们的新逻辑混淆的缓存永久重定向:
#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]
编辑2:
#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]
编辑3:
实际上,我们可以在这里使用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]