简体   繁体   中英

.htaccess redirect using GET parameters

I have already checked a lot of answers related to .htaccess redirecting, but yet I was unable to elaborate my problem:

I have an old website with blog entries addressed like this:

http://ellrohir.mzf.cz/index.php?page=blog&id=77

Now I also have a new website with "nice urls" where blog entries can be found using:

http://alois-seckar.cz/politics-blog/77

I want to "shut down" the old page and redirect any old links to new site. How to catch and transfer the old url to the new url? I still cannot figure the necessary .htaccess code up :(

Try this :

RewriteEngine on


RewriteCond %{QUERY_STRING} ^page=([^&]+)&id=([^&]+)$ [NC]
RewriteRule ^index\.php$ http://alois-seckar.cz/politics-%1/%2? [NC,L,R]

Empty question mark at the end is important to discard the orignal query strings from the destination url.

%n is part of the regex in RewriteCond, it's the part matched between ([ and ]+).

This rule will redirect

example.com/index.php?page=foo&id=bar

to

example2.com/politics-foo/bar

you can redirect to another website using .htaccess

RewriteEngine on
RewriteRule ^/index.php?page=blog&id=(.*)?$ http://example.com /politics-blog/$1 [R=301,L]

I am not good at .htaccess, so here's my php solution:

index.php

<?php
$header="location: http://alois-seckar.cz";
if((isset($_GET["page"]))&&(isset($_GET["id"])))
{
    $header.="/".$_GET["page"]."/".$GET["id"];
}
header($header);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM