2

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 !!!

2
  • There is small typo in your code, olpage instead of oldpage. Commented Jul 9, 2013 at 13:23
  • Ok but this was just an example its not about typo problem in my case. Commented Jul 9, 2013 at 13:25

3 Answers 3

1

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]
Sign up to request clarification or add additional context in comments.

2 Comments

By the way any other solution than this code to do the same thing ?
RedirectPermanent and other redirect directives ignore the querystring, so you can't make redirection based on query string with them.
0

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']

1 Comment

Thank you I'll try the other .htaccess solution
0

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]

1 Comment

Good option too. Thanks a lot !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.