...

View Full Version : 301 redirect with PHP variables in URL



keith1995
03-18-2011, 08:18 PM
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/attorneydetails.php?action=view&id=8 to http://www.domain.com/practiceareas.php?action=view&id=2

How would we do this in a .htaccess file?

keith1995
03-21-2011, 06:42 AM
Anyone got a tip/code to accomplish this?

abduraooft
03-21-2011, 07:13 AM
Try

RewriteEngine On

RewriteRule ^attorneydetails\.php(.*)$ practiceareas\.php$1 [L]

keith1995
03-21-2011, 04:39 PM
abduraooft,

This is definitely a step in the right direction, thank you for that.

This code rewrites all of the attorneydetails.php to practiceareas.php pages, we just need it done for one page, not the full group.

Also, the URL remains the original URL, can that be updated in the URL bar to reflect the actual URL it is forwarded to?

abduraooft
03-21-2011, 04:55 PM
Also, the URL remains the original URL, can that be updated in the URL bar to reflect the actual URL it is forwarded to?

RewriteRule ^attorneydetails\.php(.*)$ /practiceareas\.php$1 [R] ?

Krupski
03-21-2011, 07:18 PM
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/attorneydetails.php?action=view&id=8 to http://www.domain.com/practiceareas.php?action=view&id=2

How would we do this in a .htaccess file?

You're doing it the hard way. Simply put an index.php file in the current root of your webserver and make it point to your new site directory by placing this inside:



<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://new.url.of.your.website.com");
exit();
?>

No messing around with .htaccess or rewrite engines required.

keith1995
03-21-2011, 08:38 PM
Krupski,

I don't need to redirect to a whole new domain name, rather just redirect one dynamic page to another dynamic page.

Krupski
03-22-2011, 12:38 AM
Krupski,

I don't need to redirect to a whole new domain name, rather just redirect one dynamic page to another dynamic page.

It will still work. That's all I do is redirect one page to another. Here's my exact index.php file:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Redirecting you...</title>
</head>
<body style="background-color:#99ccff;padding:1.0em;">
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://three-dog.homelinux.com/phpbb/index.php");
exit();
?>
<div style="background-color:#fff7f0;width:50%;text-align:center;padding:3.0em;border:2px solid #336699;">
<a href="http://three-dog.homelinux.com/phpbb/index.php">
<span style="font-weight:bold;font-size:16px;">Please click this link if you are not automatically redirected</span>
</a>
</div>
</body>
</html>


See? All I do is redirect "index.php" from my web root to "webroot/phpbb/index.php".

Works fine.

domenicfz
04-11-2011, 04:28 PM
I have a landing page that I need to hit and then have that landing page redirect the user to a different page. I am stuck on keeping the URL variables in tact when the redirect happens.

htttp://www.domain1.com/test.php?variable1=test&variable2=test2

once that page is hit, I need it to redirect to lets say domain2 but keep the variables:

htttp://www.domain2.com/test.php?variable1=test&variable2=test2

munkeyboy
04-11-2011, 10:49 PM
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/attorneydetails.php?action=view&id=8 to http://www.domain.com/practiceareas.php?action=view&id=2

How would we do this in a .htaccess file?

At the very top of attorneydetails.php enter:


if( $_GET['id'] == 8 ){
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://www.domain.com/practiceareas.php?$_SERVER['QUERY_STRING']" );
exit();
}


Thanks to Krupski since I copied your code

classics40
09-23-2011, 02:05 PM
RewriteCond %{QUERY_STRING} ^id=108
RewriteCond %{QUERY_STRING} lang=fr$
RewriteRule index.php http://www.yoursite.com/nameforseo/fr/nameforseo/nameforseo? [L,R=301]


so here i want to redirect for example : index.php?id=108&page=content&lang=fr
*note that i filter variable id and language in two different rewrite conditions.

Then i redirect to any page i want. Also note the ? at the end of my rewrite rule, the ? stops the automatic transmission of previous Get variables to the new redirect url.

classics40
09-23-2011, 03:54 PM
RewriteCond %{QUERY_STRING} ^id=108
RewriteCond %{QUERY_STRING} lang=fr$
RewriteRule index.php http://www.yoursite.com/nameforseo/fr/nameforseo/nameforseo? [L,R=301]


so here i want to redirect for example : index.php?id=108&page=content&lang=fr
*note that i filter variable id and language in two different rewrite conditions.

Then i redirect to any page i want. Also note the ? at the end of my rewrite rule, the ? stops the automatic transmission of previous Get variables to the new redirect url.

if for example you have index.php?var1=232&id=108&var2=blla&lang=fr
change the
[CODE]RewriteCond %{QUERY_STRING} ^id=108
RewriteCond %{QUERY_STRING} lang=fr$

for
[CODE]RewriteCond %{QUERY_STRING} &?id=108
RewriteCond %{QUERY_STRING} lang=fr$

it will look for it where ever the var is situated in the url



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum