在.htaccess中匹配问号

时间:2016-04-22 22:03:42

标签: .htaccess

我试图为旧的Kayako帮助台网址设置301重定向。旧的网址是;

example.com/index.php?/rest_of_url

我已经设置了重写,因此网址更加漂亮example.com/rest_of_url,我尝试将所有旧网址重定向到新网址。我尝试了以下但是他们似乎没有工作;

RewriteRule ^index.php?/(.*)?$ /$1 [R=301,L]

RewriteRule ^index.php\?/(.*)?$ /$1 [R=301,L]

RewriteRule ^index.(.*)/(.*)?$ /$2 [R=301,L]

我无法弄明白。

修改

这是整个.htaccess文件

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{QUERY_STRING} ^/(.*)$
RewriteRule ^index\.php$ /%1? [R=302,L]

RewriteCond $1 !^(admin|api|console|favicon\.ico|robots\.txt|sitemap\.xml|index\.php|tiny_mce_gzip\.php|cron|onsite|staff|rss|setup|visitor|winapp|wallboard|__swift) [NC]
RewriteCond $1 !\.(jpg|jpeg|png|gif|js|css|htm|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteCond $1 !^(favicon\.ico|robots\.txt|sitemap\.xml|index\.php|tiny_mce_gzip\.php|__swift) [NC]
RewriteCond $1 !\.(jpg|jpeg|png|gif|js|css|htm|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]*)/(.*)$ $1/index.php?/$2 [L]
</IfModule>

1 个答案:

答案 0 :(得分:2)

问号后面的部分不在RewriteRule网址中 您必须使用%{QUERY_STRING}

RewriteCond %{QUERY_STRING} ^/(.*)$
RewriteRule ^index\.php$ /%1? [R=301,L]

%1是上一个(.*)中的第一个RewriteCond

在您的情况下,如果没有循环,请使用:

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