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

htaccess redirect query string to a path

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

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

这很好用,使用上面的方法我可以通过 page.php?query=/page/query/ 但是我怎样才能做到这一点,如果 page.php?query= 方法,它会自动重定向到路径 version?

我试过以下方法,但我认为我写的不正确...

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

如能帮助解决此问题,我们将不胜感激。谢谢

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

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

您可以使用以下

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= [L]