Jump to content

Dynamic Url Redirect


Recommended Posts

I've been googling for hours to no avail. I'm trying to redirect a series of dynamic URLs that are all unique to more static URLs as I complete a port to Movable Type.

 

The mapping looks like this:

(OLD)

http://www.nomadicplanet.com/ImageDetails....&CurrentImage=3

(NEW)

http://www.jordansphotos.com/archives/byEn..._sur_sunset.php

 

After all my searching, I haven't been able to figure out how to put the dynamic URL with the question mark on the left side of a .htaccess redirect.

 

Attempt #1:

Redirect 301 /ImageDetails\.aspx\?GalleryId=53&CurrentImage=3 http://www.jordansphotos.com/archives/byEn..._sur_sunset.php

 

 

Nor, have I been able to create a RewriteRule that takes the dynamic URL and maps it to the appropriate static URL.

 

Attempt #2:

RewriteEngine On

RewriteBase /

RewriteRule ^ImageDetails\.aspx\?GalleryId\=53\&CurrentImage\=3$ archives/byEntry/galleries/destinations/usa/central_california_coast/20040124_big_sur_sunset.php [R=301,L]

 

 

The other question I have is that I have about 1100 of these. And, given they are all different, is this going to be a performance problem? I was trying to avoid search engine penalties by not losing the URLs but if its going to be too difficult I could also just redirect the ImageDetails.aspx file back to the Index or something.

 

Would appreciate any suggestions!

 

Jordan Breckenridge

Link to comment
Share on other sites

I can't help with the answer to your question. It's WAY above my head. But I thought I'd bid you welcome to the forums!!

 

I'm sure somebody will be along that can give you some advice.

Link to comment
Share on other sites

Everything after the "?" is stripped from the url and placed in the QUERY_STRING variable so you would need to test against that. Maybe something like:

 

RewriteCond %{QUERY_STRING} ^GalleryId\=53\&CurrentImage\=3$

RewriteRule ^ImageDetails\.aspx$ archives/byEntry/galleries/destinations/usa/central_california_coast/20040124_big_sur_sunset.php [R=301,L]

 

1100 RewriteRules in your .htaccess certainly wouldn't be pretty though! You could maybe send all references to "ImageDetails.aspx" to a php script that could manage the redirects a bit more gracefully.

Link to comment
Share on other sites

Everything after the "?" is stripped from the url and placed in the QUERY_STRING variable so you would need to test against that. Maybe something like:

 

RewriteCond %{QUERY_STRING} ^GalleryId\=53\&CurrentImage\=3$

RewriteRule ^ImageDetails\.aspx$ archives/byEntry/galleries/destinations/usa/central_california_coast/20040124_big_sur_sunset.php [R=301,L]

 

1100 RewriteRules in your .htaccess certainly wouldn't be pretty though! You could maybe send all references to "ImageDetails.aspx" to a php script that could manage the redirects a bit more gracefully.

 

Is there a special syntax for invoking the PHP script from with htaccess?

Link to comment
Share on other sites

I would just use a RewriteRule to redirect to it.

 

>RewriteRule ^ImageDetails\.aspx$ redirect_script.php [QSA]

 

The QSA flag tells apache to re-append the QUERY_STRING to the redirected address. This would make the query string available to the php script in $_GET['GalleryId'], $_GET['CurrentImage'], etc. The script would then lookup the new location and redirect. I think the php to do a 301 redirect is:

 

>header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/newdir/newpage.htm");

 

Steven

Link to comment
Share on other sites

Thanks for all the tips. For posterity, this is what I did to get it to work:

 

.htaccess:

>RewriteEngine On
RewriteBase /
RewriteRule ^redirecturl1\.aspx$ redirect_script.php [QSA]
RewriteRule ^redirecturl2\.aspx$ redirect_script.php [QSA]
RewriteRule ^redirecturl3\.aspx$ redirect_script.php [QSA]

 

redirect_script.php:

><?php

$id= ( isset( $_GET['ID'] ) ) ? $_GET['ID']: '';
$site_url = 'http://www.xxxxxxxxx.com/';

$url_redirects = array(

'1' => 'archives/URL1',
'2' => 'archives/URL2',
'6' => 'archives/URL3',
'7' => 'archives/URL4',

);

if ( $id==  '' )
{
Header ("HTTP/1.1 301 Moved Permanently");
Header('Location: '.$site_url.'index.php');
}
else
{
$url = $site_url.$url_redirects[$id];
Header ("HTTP/1.1 301 Moved Permanently");
Header('Location: '.$url);
}

?>

Edited by breckenridgej
Link to comment
Share on other sites

I just discovered a problem with my solution (and in fact, with my entire .htaccess). URLs that do not match exactly on case are being directed to my 404 page. This includes my redirects for the URLs with query parameters and my static URLs.

 

For example:

http://www.somepage.com/page1.php?id=5323

 

is different than

http://www.somepage.com/page1.php?ID=5323

 

This is particularly problematic because Google has indexed both versions of the URL. Is there an easy way to convert the URLs to all lower case before applying my other redirection rules including my PHP script?

 

Thanks again.

Jordan

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...