htaccess redirection with querystring

隐身守侯 提交于 2020-01-17 03:14:27

问题


How can I redirect the following url http://test.com/changefile.php?filename=top-ranked-colleges

to

http://test.com/top-ranked-colleges.php using htaccess redirection.

Anybody please help me. Thanks in advance.


回答1:


Try this

RewriteCond %{QUERY_STRING} ^filename=(.*)$
RewriteRule .* http://test.com/%1.php? [R=301,L]

to change http://test.com/changefile.php?filename=top-ranked-colleges to http://test.com/top-ranked-colleges.php

RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule .* http://test.com/changefile.php?filename=$1 [L]

to change http://test.com/top-ranked-colleges.php to http://test.com/changefile.php?filename=top-ranked-colleges




回答2:


This does work:

RewriteCond %{REQUEST_URI}  ^/changefile\.php$
RewriteCond %{QUERY_STRING} ^filename=([0-9a-z_-]+)$
RewriteRule ^(.*)$ http://website.com/%1.php? [R=301,L]

Explanation

%1 comes from: RewriteCond %{QUERY_STRING} ^filename=([0-9a-z_-]+)$

Have a ? on the RewriteRule stops the original query string being added to the redirect

If the file being called is changefile.php, and it has a querystring with filename= within it, then redirect using %1 as the page to go to.

If you use RewriteBase, you will need to add this in too:

e.g:

RewriteBase /test/

RewriteCond %{REQUEST_URI}  ^/test/changefile\.php$

This doesn't work:

RewriteRule ^changefile.php?filename=([a-z0-9-]+)$ http://test.com/$1.php? [R=301,L]

This is due to the fact you cannot check the querystring with RewriteRule or RedirectMatch



来源:https://stackoverflow.com/questions/6383574/htaccess-redirection-with-querystring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!