.htaccess从动态页面url到static with variables

时间:2017-10-08 13:40:50

标签: php .htaccess mod-rewrite dynamic-url

我想创建一个动态规则(可能是.htaccess)来重定向页面:

https://www.example.com/dir/search?q=stack+overflow

https://www.example.com/dir/stack-overflow

我知道我需要一个/dir文件夹

我试过这个

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\. search?q=$1

不工作。

1 个答案:

答案 0 :(得分:1)

我认为您的意思是从/dir/search?q=stack-overflow/dir/stack-overflow的301重定向,即如果用户输入https://www.example.com/dir/search?q=stack-overflow,则浏览器网址将更改为https://www.example.com/dir/stack-overflow。问题是,/dir/stack-overflow是真正的文件夹还是文件?我不这么认为,因此你必须重新/dir/stack-overflow/dir/search?q=stack-overflow

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^q=([^&]*)
RewriteRule ^/?dir/search/?$ /dir/%1 [R=301,L]

RewriteCond %{REQUEST_URI} !^/?dir/search/?$ [NC]
RewriteRule ^/?dir/([^/?]+)/?$ /dir/search?q=$1 [L]

我不想重写,而不是删除最后两行。

相关问题