kennedypd1
asked on
301 redirect
Hi
I have changed my site to seo friendly urls and now I want to setup 301 redirect in htaccess but not sure if I can.
The links that google has indexed are like
mydomain.com/page.php?id=1 312
the new link is like
mydomain.com/directory/com pany-name/ area
The only way i am aware I can get this is to use some php to get the info for id 1312 and the put the name and area into url but am I correct that I cannot do this with htaccess.
I have 3000 odd IDs so setting a 301 for each one seems like a long way to go about things.
Is there another way?
Any help much appreciated
I have changed my site to seo friendly urls and now I want to setup 301 redirect in htaccess but not sure if I can.
The links that google has indexed are like
mydomain.com/page.php?id=1
the new link is like
mydomain.com/directory/com
The only way i am aware I can get this is to use some php to get the info for id 1312 and the put the name and area into url but am I correct that I cannot do this with htaccess.
I have 3000 odd IDs so setting a 301 for each one seems like a long way to go about things.
Is there another way?
Any help much appreciated
You may have some options within your CMS, if you run one. Otherwise, set up an internal rewrite from page.php to a new script - new_script.php. The new_script.php file should make the database checks you require, generate the appropriate URL, and issue the 301.
ASKER
Hi Steve
Thanks for the reply.
This is not a cms site.
So can I just check I understand what you are saying
I do a redirect in .htaccess for any files that are coming in with the old url (which still works on exactly the same page by the way) and send them to the newscript.php. In there I grab the variables needed to create the new seo friendly url and create a header that send it new url and creates 301.
Hope that's as clear as mud
Thanks for your help
Thanks for the reply.
This is not a cms site.
So can I just check I understand what you are saying
I do a redirect in .htaccess for any files that are coming in with the old url (which still works on exactly the same page by the way) and send them to the newscript.php. In there I grab the variables needed to create the new seo friendly url and create a header that send it new url and creates 301.
Hope that's as clear as mud
Thanks for your help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Steve
Thank you so much for that.
I had spent two days looking for an answer and got nowhere.
Looks like I am nearly there but 1 last issue that I am not sure how to deal with.
I have the rewrite in a loop.
Some background:
the original site urls were webpagedisplay.php?incomin g_id=1312
I changes this to /companies/company-name/lo cation
I did this through a rewrite that grabbed the variables from link and reverted back to webpagedisplay.php
below are the two conflicting rules
# Convert SEO URL to Webpagedisplay
RewriteRule ^companies/([A-Za-z0-9-]+) /([A-Za-z0 -9-]+)/?$ /webpagedisplay.php?name=$ 1&area=$2 [L]
# New Rule for 301
RewriteRule ^/?webpagedisplay\.php$ /company301.php [QSA,L]
Im assuming these two go backwards and forwards
Below is company301.php in case it helps to understand with sensitive info removed. This file is creating the correct url but the page is blank as I think it loops.
<?php
// session_start(), if necessary for your purposes
// get the id from the query string
$incoming_id = (int) (array_key_exists('incomin g_id',$_GE T) ? $_GET['incoming_id'] : 0);
$getcompsql = "select * from company where company_id = '$incoming_id'";
$getcompresult = mysql_query($getcompsql);
$getcomprow = mysql_fetch_array($getcomp result);
$company_name = $getcomprow['company_name' ];
$physical_address2 = $getcomprow['physical_addr ess2'];
$cleanname = str_replace(' ', '-', $company_name);
// initialize the url variable and response code
$the_url = '';
$code = 404;
// process the id to form the link
if ($incoming_id) {
// call the database
// generate the URL you want
$the_url = "http://www.mydomain.com/companies/$cleanname/$physical_address2";
}
// if $the_url is populated, you found something
// otherwise, no valid id...send them somewhere else
if ($the_url) {
$code = 301;
} else {
$the_url = "/my-404-page.php";
}
// do the actual redirect
header('Location: ' . $the_url, true, $code);
// clean up and ...
die();
Thanks
Thank you so much for that.
I had spent two days looking for an answer and got nowhere.
Looks like I am nearly there but 1 last issue that I am not sure how to deal with.
I have the rewrite in a loop.
Some background:
the original site urls were webpagedisplay.php?incomin
I changes this to /companies/company-name/lo
I did this through a rewrite that grabbed the variables from link and reverted back to webpagedisplay.php
below are the two conflicting rules
# Convert SEO URL to Webpagedisplay
RewriteRule ^companies/([A-Za-z0-9-]+)
# New Rule for 301
RewriteRule ^/?webpagedisplay\.php$ /company301.php [QSA,L]
Im assuming these two go backwards and forwards
Below is company301.php in case it helps to understand with sensitive info removed. This file is creating the correct url but the page is blank as I think it loops.
<?php
// session_start(), if necessary for your purposes
// get the id from the query string
$incoming_id = (int) (array_key_exists('incomin
$getcompsql = "select * from company where company_id = '$incoming_id'";
$getcompresult = mysql_query($getcompsql);
$getcomprow = mysql_fetch_array($getcomp
$company_name = $getcomprow['company_name'
$physical_address2 = $getcomprow['physical_addr
$cleanname = str_replace(' ', '-', $company_name);
// initialize the url variable and response code
$the_url = '';
$code = 404;
// process the id to form the link
if ($incoming_id) {
// call the database
// generate the URL you want
$the_url = "http://www.mydomain.com/companies/$cleanname/$physical_address2";
}
// if $the_url is populated, you found something
// otherwise, no valid id...send them somewhere else
if ($the_url) {
$code = 301;
} else {
$the_url = "/my-404-page.php";
}
// do the actual redirect
header('Location: ' . $the_url, true, $code);
// clean up and ...
die();
Thanks
You need to reverse the order of your two rewrites:
# New Rule for 301
RewriteRule ^/?webpagedisplay\.php$ /company301.php [QSA,L]
# Convert SEO URL to Webpagedisplay
RewriteRule ^companies/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /webpagedisplay.php?name=$1&area=$2 [L]
ASKER
Hi Steve
Reversing rules didn't seem to work but have resolved the issue by changing the name of the final php file which has stopped the loop.
Thanks for your help
Reversing rules didn't seem to work but have resolved the issue by changing the name of the final php file which has stopped the loop.
Thanks for your help