htAccess 将查询字符串重定向到路径



所以我有一个htaccess重写,使查询字符串成为这样的路径。

RewriteRule ^page/(.*)$ page.php?query=$1

这工作正常,使用上述方法,我可以通过page.php?query= 和/page/query/访问页面,但是我怎样才能做到这一点,以便如果页面被 page.php?query=方法访问,它会自动重定向到路径版本?

我已经尝试了以下内容,但我认为我写错了......

RewriteCond %{HTTP_HOST} ^www.webaddress.com/page.php?query=$
RewriteRule ^(.*)$ http://www.webaddress.com/page/$1 [R=301,L] 

任何解决此问题的帮助将不胜感激。谢谢。

您可以使用以下内容

RewriteEngine On
#url redirection from old url to the new one
#redirect /page.php?query=foo to /page/foo
RewriteCond %{THE_REQUEST} /page.php?query=([^s]+) [NC]
RewriteRule ^.+$ /page/%1? [L,R]
#url rewiring from new url to the old one
# rewrite /page/foo to /page.php?query=foo
RewriteRule ^page/(.*)$ page.php?query=$1 [L]

在内部重写规则之前尝试一下。

RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+).php
RewriteCond %{QUERY_STRING} ^(.+)=(.+)
RewriteRule ^ http://%{HTTP_HOST}/%1/%2/%3? [R=301]

最新更新