开发者

htaccess redirection with querystring

开发者 https://www.devze.com 2023-03-13 10:54 出处:网络
How can I redirect the f开发者_如何转开发ollowing url http://test.com/changefile.php?filename=top-ranked-colleges

How can I redirect the f开发者_如何转开发ollowing 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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消