301 redirection

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
imahsan
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2009 10:50 pm

301 redirection

Post 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
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: 301 redirection

Post by daedalus__ »

im not sure if you can change the case with mod rewrite but you sure can using php
imahsan
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2009 10:50 pm

Re: 301 redirection

Post by imahsan »

Can you guide me how to do that in PHP? A Code example can be extremely handy and helpful..!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: 301 redirection

Post 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;
imahsan
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2009 10:50 pm

Re: 301 redirection

Post 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 ?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: 301 redirection

Post by requinix »

imahsan wrote:but its not working
Care to elaborate?

And how about you post exactly what you have in both files.
imahsan
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2009 10:50 pm

Re: 301 redirection

Post 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 (?)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: 301 redirection

Post by requinix »

I wonder if the Rule should be

Code: Select all

RewriteRule ^/?(?!redirect.php) redirect.php [L]
imahsan
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2009 10:50 pm

Re: 301 redirection

Post by imahsan »

but when i did as you suggested its showing internal server error!!
imahsan
Forum Newbie
Posts: 6
Joined: Mon Dec 28, 2009 10:50 pm

Re: 301 redirection

Post by imahsan »

Still the same internal server error :(
Post Reply