301 redirect with PHP variables in URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
curseofthe8ball
Forum Commoner
Posts: 73
Joined: Sun Jun 01, 2003 12:33 am

301 redirect with PHP variables in URL

Post by curseofthe8ball »

We need to redirect a URL to a new URL but we can't get it to work and I believe it is due to the inclusion of variables in the URL.

For example, we need to redirect http://www.domain.com/attorneydetail...tion=view&id=8 to http://www.domain.com/practiceareas....tion=view&id=2

How would we do this in a .htaccess file?
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

Re: 301 redirect with PHP variables in URL

Post by divedj »

Try

REDIRECT 301 /attorneydetail...tion=view&id=8 http://www.domain.com/practiceareas....tion=view&id=2

Make sure you have a single space between the elements and dont include a http://www.domain.com in the first part where you tell which page to redirect.
curseofthe8ball
Forum Commoner
Posts: 73
Joined: Sun Jun 01, 2003 12:33 am

Re: 301 redirect with PHP variables in URL

Post by curseofthe8ball »

divedj,

Thanks for the reply. Unfortunately that doesn't seem to work. It works fine if I make it:

REDIRECT 301 /attorneydetails.php http://www.domain.com/practiceareas.php ... =view&id=2

But that isn't what we need. Something seems to get messed up when we pass the ?action=view&id=8 part in first URL element.
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

Re: 301 redirect with PHP variables in URL

Post by divedj »

Sorry, just learned something myself.

The REDIRECT comand only looks at path/file not at querystrings of a given URL
you may have to look into modrewrite.

RewriteEngine on
RewriteCond %{QUERY_STRING} ^([^&]&)*id=8(&|$)
RewriteRule ^$ http://www.domain.com/attorneydetail...tion=view&id=2 [R=301,L,QSA]

A pretty good basic explanation of mode rewrite you find here http://www.yourhtmlsource.com/sitemanag ... iting.html
Post Reply