mod_rewrite - 强制重定向到重写的URL

时间:2015-06-15 10:15:43

标签: apache .htaccess mod-rewrite redirect

我有以下网址[1]:

www.domain.com/?search=somequery

我想重定向到[2]

www.domain.com/search/somequery

我在.htaccess中使用以下代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/(.*)$ /?search=$1 [L]

上面的代码很好,因为服务器可以理解这两个URL:[1]和[2]。但是,如果用户访问[1] URL

,我无法弄清楚如何强制重定向到[2] URL

2 个答案:

答案 0 :(得分:3)

试试这个

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /\?search=([^&\ ]+)
RewriteRule ^ search/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/(.*)$ /?search=$1 [L]

答案 1 :(得分:1)

您需要为该重定向设置新规则:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /(?:index\.php)?\?search=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/(.*)$ /?search=$1 [L,QSA]