Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 03-13-2006, 08:46 AM
Rezo's Avatar
Rezo Rezo is offline
Senior Member
 
Join Date: 03-13-06
Location: Republic of Moldova
Posts: 256
iTrader: 0 / 0%
Thumbs up Search Engine Friendly Redirect

A 301 redirect is executed server-side which means that the visitor is never aware that they have been redirected, there is no time delay like with javascript


To use the following PHP code you must change the file extension of the file you will be using to index.php instead of index.html.
Now put the following code into your new index.php page, put ONLY this code, no need for <head>, <body> or any other tags.
Change www.newdomain.com/page.html to the url the page will be redirecting to


PHP Code:
<?php
header
("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://www.newdomain.com/page.html"); 
exit(); 
?>
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 03-13-2006, 08:55 AM
Kirk's Avatar
Kirk Kirk is offline
Junior Member
 
Join Date: 03-13-06
Location: UK
Posts: 29
iTrader: 0 / 0%
Good advice, the 301 is a permanent redirect is much better than the 302 temporary, which the search engines can be suspicious of. This is due to page the recent spate if 302 page hijacks.
 
Reply With Quote
  #3  
Old 03-13-2006, 08:55 AM
kyleposey's Avatar
kyleposey kyleposey is offline
Member
 
Join Date: 03-02-06
Location: Indianapolis, Indiana
Posts: 81
iTrader: 0 / 0%
While you are showing us how to do it in PHP, could you show us how to do it in a .htaccess file? I'm looking to update my site, and I want to permanantly move several pages, and I don't want to leave my remnants behind.
 
Reply With Quote
  #4  
Old 03-13-2006, 09:03 AM
Mong's Avatar
Mong Mong is offline
Contributing Member
 
Join Date: 03-02-06
Location: floating on the C
Posts: 502
iTrader: 0 / 0%
Code:
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\.nichewatch\.com [NC] RewriteRule ^(.*) http://www.nichewatch.com/$1 [L,R=301]
It will redirect http://nichewatch.com to http://www.nichewatch.com
 
Reply With Quote
  #5  
Old 03-13-2006, 09:11 AM
Rezo's Avatar
Rezo Rezo is offline
Senior Member
 
Join Date: 03-13-06
Location: Republic of Moldova
Posts: 256
iTrader: 0 / 0%
Quote:
Originally Posted by kyleposey
While you are showing us how to do it in PHP, could you show us how to do it in a .htaccess file? I'm looking to update my site, and I want to permanantly move several pages, and I don't want to leave my remnants behind.
Want to update your site? Tell me here what scripts do you need more, or what do you want to update, I can help you
 
Reply With Quote
  #6  
Old 09-13-2006, 07:51 PM
Jayesh Lodha Jayesh Lodha is offline
Junior Member
 
Join Date: 09-11-06
Location: India
Posts: 4
iTrader: 0 / 0%
301 redirect with subdomains

Mong,

these .htaccess code will redirect all non www pages to www pages.

But

I have following url as well in my website

http://subdomain1.mydomain.com
http://subdomain2.mydomain.com
http://subdomain3.mydomain.com and so on

So using these .htaccesscode will it be redirected to

http://www.subdomain1.mydomain.com
http://www.subdomain2.mydomain.com
http://www.subdomain3.mydomain.com

or it will redirect to the same location :

http://subdomain1.mydomain.com
http://subdomain2.mydomain.com
http://subdomain3.mydomain.com
 
Reply With Quote
  #7  
Old 09-16-2006, 10:39 AM
webzenstar's Avatar
webzenstar webzenstar is offline
Contributing Member
 
Join Date: 03-12-06
Location: Levis, QC
Posts: 243
iTrader: 0 / 0%
Thank you very much for this useful piece of code!

Many "landing page" providers might do themselves a favor by using this PHP code snippet.
 
Reply With Quote
  #8  
Old 09-23-2006, 08:24 AM
DataFeedFile.com's Avatar
DataFeedFile.com DataFeedFile.com is offline
Junior Member
 
Join Date: 09-21-06
Posts: 19
iTrader: 0 / 0%
from my experince... this is a half-way solution only....

I have used this 301 server side redirect...
it is better than PHP script redirect, but I seem to have lost
several may needed info from the original referring page / site,
such as original domain name ('script_uri'), referral IP and more...

For the best effect, just change the actual DNS server record!

But it is still a good solution in between I guess.
 
Reply With Quote
  #9  
Old 09-27-2006, 08:54 AM
lordspace's Avatar
lordspace lordspace is offline
Contributing Member
 
Join Date: 05-30-06
Location: Canada
Posts: 913
iTrader: 0 / 0%
I use following code when sending 301 header.

PHP Code:
header( (php_sapi_name()=='cgi' 'Status:' 'HTTP/1.1') . ' 301 Moved Permanently'); 
Depending on php whether it is installed as CGI the script sends Status: otherwise (module) HTTP/1.1.

Svet
 
Reply With Quote
  #10  
Old 09-27-2006, 10:12 AM
StupidScript's Avatar
StupidScript StupidScript is offline
Senior Member
 
Join Date: 09-22-06
Location: Los Angeles
Posts: 663
iTrader: 0 / 0%
One page redirect (to a different page file name):
Code:
Redirect 301 /old.html http://www.example.com/new.html
Entire domain redirect (using the same page filenames):
Code:
Redirect 301 / http://www.example.com/
There are other things you can do, like rewrite filenames and extensions, but those two are the basic .htaccess 301 redirect methods.

If you have access to the httpd.conf file, you can also use the same code above within a <Directory> or <VirtualHost> statement. There is no serious advantage to using .htaccess files or building the redirect into the Apache configuration file, so ... whatever floats your boat.

And a 301 (permanent) redirect is more desireable than other types (i.e. 302 - temporary) because search engines will do more than just re-do their indexes; Google, for example, will also maintain the PageRank of a page that has been 301'd, while a 302 redirect will mess with your rankings pretty substantially.

<edit>To correct/expand on that last statement: A 301 will not harm your PageRank if the file name remains the same. Redirecting to a new file name will be handled as any new page is handled.</edit>

Last edited by StupidScript; 09-27-2006 at 10:39 AM.
 
Reply With Quote
  #11  
Old 09-27-2006, 12:38 PM
bwb's Avatar
bwb bwb is offline
Contributing Member
 
Join Date: 07-21-05
Location: USA
Posts: 57
iTrader: 0 / 0%
Is Redirect 301 /old.html http://www.example.com/new.html

to be used in a .htaccess file? Or where does that go?
 
Reply With Quote
  #12  
Old 09-27-2006, 01:00 PM
StupidScript's Avatar
StupidScript StupidScript is offline
Senior Member
 
Join Date: 09-22-06
Location: Los Angeles
Posts: 663
iTrader: 0 / 0%
Yep. Either in an .htaccess file or in a <Directory> or <VirtualHost> statement in the Apache config file.

For the heck of it:

1) Create a new, empty text file named .htaccess and place it in the root directory of your website. Note that the entire file name is 'dot'-htaccess, there is nothing preceding the 'dot'.

2) Open the text file and type in:
Redirect 301 /old.html http://www.example.com/new.html
where "old.html" is the name of the old file, "example.com" is your domain name and "new.html" is the name of the new file. It can be the same name, or completely different with a different file extension, if needed. Make certain that there are (a) no blank lines preceding the first entry or between entries and (b) no space characters preceding the word "Redirect" on any line. Be sure to include a "relative path" to the old file on each line and a fully-qualified path to the new file on each line, as illustrated. In the example above, "old.html" is stored in the "document root", right alongside the .htaccess file.

3) Save the file and quit your editing program.
(If you create the text file on your desktop and plan to upload it to your server, you _must_ transfer the file in ASCII mode, or there will be trouble ... )

4) Make sure the permissions on the file are at least 644 (rw-r--r--) so it can be read by requesting agents.

That's it. You can add additional lines for other files.

Note: .htaccess files only work with an Apache web server, and the server configuration file needs to allow use of such files by including the "AllowOverride" parameter in the appropriate config statement, i.e.:

<Directory "/var/www/html">
Options AllowOverride
</Directory>

Regarding placing the instruction in the config file, you'd use something like:

<Directory "/var/www/html">
Redirect 301 /old.html http://www.example.com/new.html
</Directory>

in which case the .htaccess file is not necessary to accomplish the goal.

Last edited by StupidScript; 09-27-2006 at 01:05 PM.
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re-write Url to make website more friendly for search engine. haga SEO Forum 2 09-26-2006 12:37 AM
Search engine friendly box always displays at bottom of screen Jayesh Lodha Coding Forum 3 09-13-2006 06:10 PM
Search Engine Friendly Redirect - 301 Rezo Coding Forum 0 08-05-2006 01:17 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 07:58 AM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC