Page 1 of 1

301 redirection

Posted: Mon Dec 28, 2009 11:23 pm
by imahsan
Hello Friends.. how r u all? Ahsan here.. am a newbie to PHP... I am stuck in a problem and I think this is the best platform for discussing issues related to PHP and getting quality solutions..Okay now lets talk about the issue.. I have a website of which i changed the domain name recently.. so now I want all my website visitors, that are requesting a particular page (or the index page) on the old domain, to be redirected (permanently i.e. with 301 header) to the corresponding page on the new domain but with the lower case i.e http://www.olddomain.com/AHSAN/ is redirected to http://www.newdomain.com/ahsan/. In addition to this, I also want to have my old domain in Google webmaster tools i.e. to verify the ownership of my old domain. Now what I did is that I made a .htaccess file as follows :



Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]



The redirection is working fine but the script is not lowering the case of the URL i.e. http://www.olddomain.com/AHSAN/ is redirected to http://www.newdomain.com/AHSAN/ instead of http://www.newdomain.com/ahsan/ and also Google is not able to verify the ownership of mine for my old domain.. Here I want to mention that I am verifying my website through the "uploading file" method so when google tries to access that file on the old domain it is redirected to the new domain. I hope I am clear with my query.. If I am not, please let me know.. Hope to get the solution soon.

Regards
Ahsan

Re: 301 redirection

Posted: Tue Dec 29, 2009 1:06 am
by daedalus__
im not sure if you can change the case with mod rewrite but you sure can using php

Re: 301 redirection

Posted: Tue Dec 29, 2009 1:11 am
by imahsan
Can you guide me how to do that in PHP? A Code example can be extremely handy and helpful..!

Re: 301 redirection

Posted: Tue Dec 29, 2009 2:23 am
by requinix
To route everything through PHP,

Code: Select all

RewriteRule ^/?(?!redirect.php) /redirect.php [L]
and then

Code: Select all

<?php
 
$request = $_SERVER["REQUEST_URI"];
$request = strtok($request, "?") . "?" . strtok(""); // only lowercase the path
$request = strtolower(rtrim($request, "?")); // remove any trailing ?s
 
header("Location: http://www.newdomain.com{$request}");
header("HTTP/1.1 301 Moved Permanently");
 
exit;

Re: 301 redirection

Posted: Tue Dec 29, 2009 3:06 am
by imahsan
I put

Code: Select all

RewriteRule ^/?(?!redirect.php) /redirect.php [L]
in .htaccess


and

Code: Select all

<?php
 
$request = $_SERVER["REQUEST_URI"];
$request = strtok($request, "?") . "?" . strtok(""); // only lowercase the path
$request = strtolower(rtrim($request, "?")); // remove any trailing ?s
 
header("Location: http://www.newdomain.com{$request}");
header("HTTP/1.1 301 Moved Permanently");
 
exit;
?>
in redirect.php


but its not working :( any other suggestion ?

Re: 301 redirection

Posted: Tue Dec 29, 2009 5:25 am
by requinix
imahsan wrote:but its not working
Care to elaborate?

And how about you post exactly what you have in both files.

Re: 301 redirection

Posted: Tue Dec 29, 2009 5:52 am
by imahsan
Temporarily my .htaccess file look as follows:

Code: Select all

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

With the above code redirection is working fine but not with the lower case and also Google verification issue.

Then i replace my code with what you suggested, that made my .htaccess file as follows:

Code: Select all

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/?(?!redirect.php) /redirect.php [L]
and redirect.php file as :

Code: Select all

<?php
 
$request = $_SERVER["REQUEST_URI"];
$request = strtok($request, "?") . "?" . strtok(""); // only lowercase the path
$request = strtolower(rtrim($request, "?")); // remove any trailing ?s
 
header("Location: http://www.newdomain.com{$request}");
header("HTTP/1.1 301 Moved Permanently");
 
exit;
?>

but its showing:

500 Internal Server Error (?)

Re: 301 redirection

Posted: Tue Dec 29, 2009 6:26 am
by requinix
I wonder if the Rule should be

Code: Select all

RewriteRule ^/?(?!redirect.php) redirect.php [L]

Re: 301 redirection

Posted: Tue Dec 29, 2009 6:32 am
by imahsan
but when i did as you suggested its showing internal server error!!

Re: 301 redirection

Posted: Tue Dec 29, 2009 6:40 am
by imahsan
Still the same internal server error :(