user1829621
user1829621

Reputation: 31

.htaccess and .php? redirection

I'm editing a .htaccess file for redirection from www.oldwebsite.com to www.newwebsite.com + specific redirection for pages.

the following code is working good :

RedirectPermanent /page.html http://www.newwebsite.com/newpage.html

Where I got problem is when I try to redirect pages ending like this with redirectpermanent :

oldpage.php?id=1

At this point I get a 404 error back.

I tried another solution that is this code

RewriteCond %{QUERY_STRING}   ^id=1$
RewriteRule ^oldpage\.php$ http://www.newwebsite.com/newpage2.html [R=301,L]

This is working excepted the browser makes me go to the following link :

http://www.newwebsite.com/newpage2.html?id=1

Can someone help me with this issue. I would like to use Redirect permanent (doesnt work, same thing with Redirect seeother). I think solution is easy but I don't get a detail I think.

Thanks !!!

Upvotes: 2

Views: 108

Answers (3)

miah
miah

Reputation: 10433

If you are trying to drop the query string, you can use the QSD flag.

RewriteCond %{QUERY_STRING}   ^id=1$
RewriteRule ^oldpage\.php$ http://www.newwebsite.com/newpage2.html? [R=301,L, QSD]

Upvotes: 0

Ahmed Habib
Ahmed Habib

Reputation: 189

You can use PHP $_SERVER['PHP_SELF'] method for this..

so,

$page = $_SERVER['PHP_SELF'];

header("Location: http://www.newwebsite.com$page");

It will redirect http://www.oldwebsite.com/newpage.html to http://www.newwebsite.com/newpage.html

or instead of $_SERVER['PHP_SELF'] you can try $_SERVER['REQUEST_URL']

Upvotes: 0

Amine Hajyoussef
Amine Hajyoussef

Reputation: 4430

just add ? at the end of rewriteRule to override query string

RewriteCond %{QUERY_STRING}   ^id=1$
RewriteRule ^oldpage\.php$ http://www.newwebsite.com/newpage2.html? [R=301,L]

Upvotes: 1

Related Questions