.htaccess, 301 Redirects & SEO: Guest Post by NotSleepy

Tony Spencer here doing a guest spot on SEOBook. Aaron was asking me some 301 redirect questions a while back and recently asked me if I would drop in for some
tips on common scenarios so here goes. Feel free to drop me any questions in the comments box.

301 non-www to www

From what I can tell Google has yet to clean up the canonicalization problem that arises when the www version of your site gets indexed along with the non-www version (i.e. http://www.seobook.com & http://seobook.com).

<code>
RewriteEngine On

RewriteCond %{HTTP_HOST} ^seobook.com [NC]
RewriteRule ^(.*)$ http://www.seobook.com/$1 [L,R=301]
</code>

The '(*.)$' says that we'll take anything that comes after http://seobook.com and append it to the end of 'http://www.seobook.com' (thats the '$1' part) and redirect to that URL. For more grit on how this works checkout a good regular expressions resource or two.

Note: You only have to enter 'RewriteEngine On' once at the top of your .htaccess file.

Alternately you may chose to do this 301 redirect from
in the Apache config file httpd.conf.

<code>
<VirtualHost 67.xx.xx.xx>
ServerName www.seobook.com
ServerAdmin webmaster@seobook.com
DocumentRoot /home/seobook/public_html
</VirtualHost>

<VirtualHost 67.xx.xx.xx>
ServerName seobook.com
RedirectMatch permanent ^/(.*) http://www.seobook.com/$1
</VirtualHost>
</code>

Note that often webhost managers like CPanel would have placed a 'ServerAlias' seobook.com in the first VirtualHost entry which would negate the following VirtualHost so be sure to remove the non-www ServerAlias.

301 www to non-www

Finally the www 301 redirect to non-www version would look like:

<code>
RewriteCond %{HTTP_HOST} ^www.seobook.com [NC]
RewriteRule ^(.*)$ http://seobook.com/$1 [L,R=301]
</code>

Redirect All Files in a Folder to One File

Lets say you no longer carry 'Super Hot Product' and hence want to redirect all requests to the folder /superhotproduct to a single page called /new-hot-stuff.php. This redirect can be accomplished easily by adding the following your .htaccess page:

<code>
RewriteRule ^superhotproduct(.*)$ /new-hot-stuff.php [L,R=301]
</code>

But what if you want to do the same as the above example EXCEPT for one file? In the next example all files from /superhotproduct/ folder will redirect to the /new-hot-stuff.php file EXCEPT /superhotproduct/tony.html which will redirect to /imakemoney.html

<code>
RewriteRule ^superhotproduct/tony.html /imakemoney.html [L,R=301]
RewriteRule ^superhotproduct(.*)$ /new-hot-stuff.php [L,R=301]
</code>

Redirect a Dynamic URL to a New Single File

It's common that one will need to redirect dynamic URL's with parameters to single
static file:

<code>
RewriteRule ^article.jsp?id=(.*)$ /latestnews.htm [L,R=301]
</code>

In the above example, a request to a dynamic URL such as http://www.seobook.com/article.jsp?id=8932
will be redirected to http://www.seobook.com/latestnews.htm

SSL https to http

This one is more difficult but I have experienced serious canonicalization problems
when the secure https version of my site was fully indexed along side my http version. I have yet
to find a way to redirect https for the bots only so the only solution I have for now is
to attempt to tell the bots not to index the https version. There are only two ways I know to do this and neither are pretty.

1. Create the following PHP file and include it at the top of each page:

if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
echo '<meta name="robots" content="noindex,nofollow">'. "\n";
}

2. Cloak your robots.txt file.
If a visitor comes from https and happens to be one of the known bots such as googlebot, you will display:

User-agent: *
Disallow: /

Otherwise display your normal robots.txt. To do this you'll need to alter your .htaccess
file treat .txt files as PHP or some other dynamic language and then proceed to write
the cloaking code.

I really wish the search engines would get together and add a new attribute to robots.txt
that would allow us to stop them from indexing https URLs.

Getting Spammy With it!!!

Ok, maybe you aren't getting spammy with it but you just need to redirect a shit ton of pages. First of all it'll take you a long time to type them into .htaccess, secondly too many entries in .htaccess tend to slow Apache down, and third its too prone to human error. So hire a programmer and do some dynamic redirecting from code.

The following example is in PHP but is easy to do with any language. Lets say you switched to a new system and all files that ended in the old id need to be redirected. First create a database table that will hold the old id and the new URL to redirect to:

old_id INT
new_url VARCHAR (255)

Next, write code to populate it with your old id's and your new URLs.

Next, add the following line to .htaccess:

<code>
RewriteRule ^/product-(.*)_([0-9]+).php /redirectold.php?productid=$2
</code>

Then create the PHP file redirectold.php which will handle the 301:

<code>
<?php
function getRedirectUrl($productid) {
// Connect to the database
$dServer = "localhost";
$dDb = "mydbname";
$dUser = "mydb_user";
$dPass = "password";

$s = @mysql_connect($dServer, $dUser, $dPass)
or die("Couldn't connect to database server");

@mysql_select_db($dDb, $s)
or die("Couldn't connect to database");

$query = "SELECT new_url FROM redirects WHERE old_id = ". $productid;
mysql_query($query);
$result = mysql_query($query);
$hasRecords = mysql_num_rows($result) == 0 ? false : true;
if (!$hasRecords) {
$ret = 'http://www.yoursite.com/';
} else {
while($row = mysql_fetch_array($result))
{
$ret = 'http://www.yoursite.com/'. $row["new_url"];
}
}
mysql_close($s);
return $ret;
}

$productid = $_GET["productid"];
$url = getRedirectUrl($productid);

header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
exit();
?>
</code>

Now, all requests to your old URLs will call redirectold.php which will lookup the new URL and return a HTTP response 301 redirect to your new URL.

¿Tiene preguntas?

Questions? Ask them here and I'll do what I can.

Published: June 7, 2006 by Aaron Wall in technology

Comments

June 22, 2006 - 7:34pm

I am a little new to doing redirects and have a client that wants to redirect an entire subdomain to a folder within their main domain without losing a lot of the backlinks they have to the subdomain. They have a lot of content under the subdomain.

What I mean is they want to move averything under subdomain.domain.com to domain.com/subdomain/

Any ideas on how I should go about doing this?

Jill
August 6, 2007 - 11:59am

I redesigned a website and discarded all the old filenames which mostly had spaces in the filenames. I added Redirect 301 lines into the .htaccess file. This works perfectly for the 3 files contact.htm index.htm and retreats.htm. All the other files have names like why nm.htm. I have tried using this name or why%20nm.htm and various alternatives with quotes and double quotes. But I can't get anything to work. How can I get around this?

Jill
August 6, 2007 - 12:00pm

I redesigned a website and discarded all the old filenames which mostly had spaces in the filenames. I added Redirect 301 lines into the .htaccess file. This works perfectly for the 3 files contact.htm index.htm and retreats.htm. All the other files have names like why nm.htm. I have tried using this name or why%20nm.htm and various alternatives with quotes and double quotes. But I can't get anything to work. How can I get around this?

Jill
August 6, 2007 - 12:32pm

Spoke too soon. I found the answer. Put my quotes in the wrong spot, needed to be before the slash not after it.
Redirect 301 "/why nm.htm" http......

June 23, 2006 - 12:25pm

Great article. Thanks. I have an additional question.

I already use code to 301 domainname.com to www.domainname.com/

What I like to know is what code i need to add to make a type in/back link like www.domainname.com/index.php redirect to www.domainname.com

Any suggestion would be great as I have shit searched myself in GG etc and havenn't found anything (probably i have to search I better:)

July 7, 2007 - 2:34am

Another friend offered these .htaccess tips... though I have not verified all of them yet.

To redirect all URLs with variables to one location try this


RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.+)$ http://www.siteurl.com/$1? [L,R=301]

to strip all parameters from a folder


RewriteCond %{QUERY_STRING} .+
RewriteRule ^(/folderwithstuff/?.*)$ http://www.site.com/newfolder/$1? [NC,L,R=301]

Deborah
June 23, 2006 - 3:43pm

Thanks for the info on .htaccess. I've been using it for years, but always find something new that I've missed.

Naive question; I've only worked with Apache/Unix systems before, but how do you do a redirect under a Windows server?

Jeff
November 20, 2006 - 11:44pm

What I'm trying to do is 301:

http://www.support.domain.com

to

http://support.domain.com

I've tried the script above but it just brings down my site to garbled text. Is there something I need to do because I'm using subdomains, or could anyone email me the correct script I should use?

Thanks!

Jeff

June 23, 2006 - 4:28pm

Hi there,

I changed a url parameter (prop_ID to property) on my site, it's changed for almost 3000 url's, how can i redirect old urls to new ones?

sample old url
www.mysite.com/detail.asp?prop_ID=178

New url is
www.mysite.com/detail.asp?property=178

thanks guys

June 23, 2006 - 6:01pm

Thanks for this very nice post !
About the https stuff, you can also do something like this :

if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
$nohttps='http://www.domain.tld/rep/';
if (isset($_SERVER['QUERY_STRING'])&&($_SERVER['QUERY_STRING']!=''))
$nohttps.='?'.substr($_SERVER['QUERY_STRING'],0,2048);
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$nohttps);
}

pairodimes
April 6, 2007 - 12:05am

I looked over a few .htaccess Redirect tutorials - but I just did not seem to find the solution to what I think should be an easy fix.

I have a Blog installed - and the pages are indexed in search engines with /index.php?category=(value)Itemid=(value)

I am in the process of rebuilding my site. The directory structure is way diferent now. Mainly they are all static index.php pages. Now that I am on a server that has APACHE and .htaccess I want to Map my pages from the existing old site -> to my new static /index.php pages.

Example: www.mywebsite.com/website/index.php?category=2Itemid=3
-> www.mywebsite.com/about/(defaulting to index.php)

I am maping this out based on Itemid=(value)
ex: Itemid=3 => /about/
Itemid=4 => /portfolio/

Thank you in advance for any advice you might have.
P.S. I bought your book a year or two ago and it has been exponentialy inspirational.

June 7, 2006 - 7:12am

How about redirecting from index.html to www or www to index.html. Which do you recommend if any?

Thanks

August 19, 2006 - 1:40am

So how many rewrites does it take to slow down apache?

Have about 3,000 301's to do to a new folder structure.

Thanks!

feet
March 19, 2007 - 8:44am

I am converting a site that is all .htm and .shtml to .net(.aspx). What is the best way to redirect to the new .net pages.

rtpHarry
June 7, 2006 - 10:38am

I would say redirect index.htm -> www because if you use your site in print advertising, or if somebody is linking to your front page then you're only going to use the straight domain.

June 7, 2006 - 2:48pm

Hi MikeTheInternetGuy,
Yeah, first off I would structure your site to always internally link to the relative root folder versus /index.htm. I believe MC has recommended this at one time.

To do the index.htm to www:
RewriteRule http://www.seobook.com/index.htm http://www.seobook.com [L,R=301]

But that only gets the homepage. I admit I'm not expert at regular expressions so if anyone knows how to redirect all subfolder/index.htm to subfolder/ lets hear it.

Nick Kisberg
June 7, 2006 - 5:11pm

Very handy article Tony.

Something else that could be useful is how do you redirect one directory to a new one with a 301,

E.g

/foo/ to /foo/bar

June 7, 2006 - 5:18pm

Thanks for the post. Just want I needed.

June 7, 2006 - 5:37pm

Thanks, great info! I do have a couple questions I can't seem to get a clear answer on:
If, when redesigning your site, you go from html to php, what's the best way to redirect. So you would have non-www and index.html, and index.php all redirecting to www. And the other file.html redirecting to file.php.

Secondly, what if you have a subdomain, and you'd like to redirect subdomain.domain.com to subdomain.domain.com/dirname?

June 7, 2006 - 6:16pm

I have a better (more generic) version of non-www to www:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

I actually have a "template" .htaccess file with these 3 lines and just copy-paste it in each new site I have. I never have to modify the code.

Larry Fowler
July 30, 2007 - 6:26pm

I have a web site which is pointed to by 2 different URLs. For instance aaa.com and bbb.com. My desire is to differentiate between the two using an .htaccess file but have been having trouble. The aaa.com URL takes me to index.html and I want bbb.com to take me to a different file. I was given the following code which doesn't work,

RewriteEngine On

RewriteCond %{HTTP_HOST} bbb.com
RewriteCond %{REQUEST_URI} !bbb/
RewriteRule ^(.*)$ newfile.html

I am still routed to index.html. Could you help.

linkshark
June 8, 2006 - 1:18am

Hey Tony,

What timing -- I am actually redirecting some old .cgi pages from a long ago deleted script to regain the old backlinks that are out there.

"It is in your best interests to not spam this blog."
How's that working Aaron?

I mostly use captcha ;-)

Earnest
June 14, 2007 - 6:31pm

PLEASE HELP!!

I would like to permanently move
huntsvillepr.com/links/index.php?category=Dentists

to this
huntsvillepr.com/dir/dentists.php

and
huntsvillepr.com/links/index.php?category=Accounting+%26+Taxes

to this
huntsvillepr.com/dir/accounting.php

What are the exact 301 .htaccess code that i should use

Anil
June 8, 2006 - 4:59am

Hi

it is not working at my site, please guide

RewriteRule http://www.seobook.com/index.htm http://www.seobook.com [L,R=301]

Furqan Durvesh
June 8, 2006 - 3:35pm

I really understand the usage of 301 direct and thanks for expert coding by moving old urls to new URls. we are in coding to move our asp pages to .aspx and i need this script in ASPX coding because our site is well indexes in 3 Major Search Engines. Hope this can work and handle all previous URLs and switch to new ones at apnahyderabad.com

June 9, 2006 - 3:01pm

Great post Tony!

Furqan, if you are using .NET on a Windows server, your best bet is to intall the ISAPI_Rewrite filter...

http://www.isapirewrite.com/

This will allow you to perform the same types of redirects server-wide.

Discount Travel
September 25, 2006 - 4:26pm

Question, Is it necessary to place [R=301,L] after each entry or is it sufficent to place [R=301] and the L just after the final entry? Very confused

Bernie
December 29, 2006 - 8:04pm

Thank you for the great article. A true reference, which I've bookmarked.

In my quest to squash duplicate URLs I have tried to find a way to redirect many URLs at once using regular expressions, but can't seem to do it. Essentially, I would like to change the very end of the URL from an "M" or "C29" to a "C." Here's what I would like to achieve:

Redirect these two URLs:
http://www.example.com/widgets-more/2433_0_2_0_M/
http://www.example.com/widgets-more/2433_0_2_0_C29/
To:
http://www.example.com/widgets-more/2433_0_2_0_C/

The 4-digit number (2433) is the only thing that keeps changing for each entry/URL. Any help greatly appreciated. Thank you.

June 10, 2006 - 1:23am

It's very important to use [NE] flag for this kind of redirects. Example:

RewriteCond %{HTTP_HOST} ^dgx\.cz$
RewriteRule ^.*$ http://www. dgx.cz/$0 [R=permanent,NE,L]

NE means 'noescape' and it remains query string unchanged.

August 22, 2007 - 2:00am

Nevermind. I figured it out.

December 1, 2006 - 2:34am

Jeff - if you still need help you can try my example (as I use the non-www version as well). I had a similar problem when trying to use that setup & things didn't work. This might help.

http://www.jimwestergren.com/wordpress-users-sharpen-your-urls-with-goog...

Jayesh Lodha
November 3, 2006 - 5:50pm

This problem I am facing is highly technical in nature. I do not have any clue. I read many forums online but did not came to conclusion.

Even though my posting is too long which is because I have mentioned each and every facts about my problem so that It will be easy to make you understand what problem I am facing.

Kindly Read it very carefully.

Kindly help me.

I have following subdomains , domains and files.

http://www.mydomain.com/index.htm
http://www.mydomain.com/1.htm
http://www.mydomain.com/2.htm

http://subdomain1.mydomain.com/index.htm
http://subdomain1.mydomain.com/1.htm
http://subdomain1.mydomain.com/2.htm

http://subdomain2.mydomain.com/index.htm
http://subdomain2.mydomain.com/1.htm
http://subdomain2.mydomain.com/2.htm

-----------

I want to set following 301 permanent redirection using .htaccess :
( non www to www )

http://mydomain.com/
must redirects to
http://www.mydomain.com/

http://mydomain.com/index.htm
must redirects to
http://www.mydomain.com/

http://mydomain.com/allfile.htm
must redirects to
http://www.mydomain.com/allfile.htm

http://mydomain.com/allfolder/allfile.htm
must redirects to
http://www.mydomain.com/allfolder/allfile.htm

-----------

I also want to set up following 301 permanent redirection using .htaccess :
( www to non www for subdomains )

http://www.subdomain1.mydomain.com/index.htm
must redirect to
http://subdomain1.mydomain.com/

http://www.subdomain1.mydomain.com/allfile.htm
must redirect to
http://subdomain1.mydomain.com/allfile.htm

and

http://www.subdomain1.mydomain.com/allfolder/allfile.htm
must redirect to
http://domain1.mydomain.com/allfolder/allfile.htm

------------

I also want to set up following 301 permanent redirection using .htaccess :
( www to non www for subdomains )

http://www.subdomain2.mydomain.com/index.htm
must redirect to
http://subdomain2.mydomain.com/

http://www.domain2.mydomain.com/allfile.htm
must redirect to
http://domain2.mydomain.com/allfile.htm

and

http://www.domain2.mydomain.com/allfolder/allfile.htm
must redirect to
http://domain2.mydomain.com/allfolder/allfile.htm

-------------

I HAVE GOT FOLLWING DIFFERENT SOURCE CODES AT DIFFERENT FORUMS ONLINE BUT I DO NOT KNOW WHICH ONE IS CORRECT AND WHICH ONE IS NOT SINCE I DO NOT HAVE WORKED ON .HTACCESS BEFORE IN MY LIFETIME. NEITHER I KNOW APACHE AND LINUX.

So kindly provide me Source Code which is suitable for my requirement. Also let me know whether it is safe for search engines or not ?

To go from the non 'www' to 'www' use this code:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

-----------------------------------------------------------------------------------

To go from the 'www' to non 'www' use this code:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]

=================================================================

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^digitallywise\.com
RewriteRule ^(.*)$ http://www.digitallywise.com/$1 [R=permanent,L]

=================================================================

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www.hwtskins.co.uk$
RewriteRule ^(.*)$ http://www.hwtskins.co.uk/$1

=================================================================

So let me know the exact source code which I can use for this purpose.

It will be great help.

Jeff
September 1, 2006 - 12:50am

If you have a dynamic URL how do the bots know what the parameters are?

For example, I have:
...?state=ca&id=C12345
...?state=ca&id=C23456
...?state=ca&id=C45678

How do the bots know that the options are not
...?state=ca&id=apples
...?state=ca&id=oranges
etc.?

(Sorry! I'm new at this... and really stuck on this subject)

June 12, 2006 - 10:56am

It didnt work for me. Im getting this error:

500 Internal Server Error

What should i do?

July 19, 2006 - 6:27pm

Hi,
I have hosted our domain with yahoo & it does not give us the permission to upload the .htaccess file for permanent redirection of globalintegra.com to www. globalintegra.com & when i call or write to the yahoo support they don't have any idea about this. Can you help me if there is other way to solve this problem

junter
July 7, 2006 - 10:06pm

Hello

I would like to redirect any domain to a folder, but I can't get it working.

For example:

abc.mysite.com ... redirected to mysite.com/users/abc
defg.mysite.com ... redirected to mysite.com/users/defg

But still showing the nice url (abc.mysite.com, defg.mysite.com) in the adress bar of navigator.

Thanks in advance!!

01 Search Engin...
October 1, 2006 - 10:14am

I am using 301 redirect for my clients since long but I faced this kind of situation first time.

I have redirected olddomain.com of my client website to newdomain.com using 301 redirect ( .htaccess ) and deleted all the files from olddomain.com and copied to newdomain.com so that google, yahoo etc can not think it is duplicate content.

Even though those files are not existing in olddomain.com right now ( olddomain.com hosting space is expired right now ) but google and yahoo is still showing those olddomain.com files in their index and have not indexed new files.

So I have resubmitted those old urls to google and yahoo assuming that they will come to olddomain.com and will transfer those files and page ranks to newdomains.com but nothing happened.

So any solutions please.

uk bingo at bin...
October 11, 2006 - 7:30pm

Hi,

I have url rewrites set-up to map a static name e.g. foxy-bingo.php to a dynamic url such as /bingo-review.php?id=5&bingo-room=foxy+bingo .

However, now both urls are in the google index, I'd like to perform a 301 redirect from the dynamic name to the static name, which is then of course rewritten back to the dynamic name. Currently, google is clearly penalising the duplicate pages and starting to stop cachine them.

Is it feasible to simply have something like:

RewriteRule ^online-bingo-review.php?bingo_room=1&online_bingo_hall=Think+Bingo /think-bingo.php [N,R=301]

RewriteRule think-bingo.php online-bingo-review.php?bingo_room=1 [L]

Currently, the first line isn't working, it just loads the online-bingo-review page and seems to ignore the 301.

Thanks

sometimes, manually editing the .htaccess will results in server problems, especially if using cpanelx .. we had tiz a few times already ... duno how to resolve also

01 Search Engin...
October 2, 2006 - 2:27am

Hunox,

Your generic version is :

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

But It should be something like this as below.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

I want to know whether second code is correct or not.

December 21, 2006 - 12:22pm

Regarding the SSL / Non-SSL:

This is the best .htaccess solution I know:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

to always redirect to HTTPS, and vice versa:

RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

Tobias

Leif
December 21, 2006 - 5:33pm

How would you do a redirect in the .htaccess to redirect

http://www.somesite.com to http://www.anothersite.com

because this obviously does not work:

RewriteRul http://www.somesite.com http://www.anothersite.com

_bullseye_
November 21, 2007 - 8:46pm

I used the redirectold.php script, and it works, because:

globalamericaninc.com/redrectold.php?id=850
is redirected to
globalamericaninc.com/product_info.php?products_id=3302020

But I can't seem to get the Redirect line in the .htaccess file right. I've got this:

RewriteRule ^/new_spec2\.php?id=(.*)$ /redirectold.php?productid=$1 [R=301,L]

But it's not working. Can someone help me?

bigbyte
December 28, 2007 - 9:20pm

Is it actually safe to get a 301 redirect from a domain punished by Google or will that hurt the receiving domain (the one the redirect is pointing to)?

December 28, 2007 - 10:31pm

Hi Bigbyte
I think you can test it on a neutral third party throw away domain before testing it on one of your good sites.

If it is easy to connect you as the owner of the penalized site and the new site then they may penalize the new site even if it did nothing wrong other than have the other domain redirected at it, but that sort of penalty might be the sort of thing that is applied by hand after the fact.

shuffm1
March 27, 2008 - 1:35pm

Aaron. I was trying to work a 301 rewrite yesterday...let's just say it was "one of those days." Had trouble with it, and I did a few searches to help me figure it out. Found some good stuff, but nothing good enough. Woke up this morning with fresh eyes, and I found this on your site. Was perfect. SEO Book is becoming quite a go-to resource for me lately. Excellent post.

March 27, 2008 - 6:44pm

Glad you liked the post. It was by NotSleepy though. So make sure to tell him thanks as well. :)

alchemyv
October 13, 2008 - 3:37pm

Loved the article and came across it whilst searching in Google for too many redirects. So according to Tony, too many redirects in the HTACCESS is not advisable as it slows the server down. This probably makes sense as I was told by a leading international agency in the UK recently that thousands of redirects would lead to a drop in the rankings. But they wouldnt say why - could this be it? (assuming thats how they'd go about it. Because other pro-SEOs I spoke too couldnt believe what I told them. Because I always thought that 301s are defacto for preserving a platform upgrade or if you're really cautious then use 302s to root and then wait until the new stronger pages start ranking and then the turn the 302s into 301s. Any comments?

October 13, 2008 - 7:06pm

Correct...lots of redirects can slow down the page load time significantly.

hendrylee
October 17, 2008 - 12:24pm

I've noticed many bloggers use a plugin to do simple 301 redirect from www to non-www or vice versa.

This slows down the server, because it has to go through the entire blog core and execute the plugin instead of using the built-in web server feature.

I have a tutorial for domain redirection. The code is for Apache, nginx and lighttpd.

cooking
October 23, 2008 - 4:48am

Redirect URLs ending in /index.php or /index.html to /

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html)\ HTTP
RewriteRule ^(.*)index\.(php|html)$ /$1 [R=301,L]

australianfranchises
October 23, 2008 - 8:24am

Lot of thanks.

EricHerboso
March 23, 2009 - 7:28pm

Could it be that you used code that referenced seobook.com on purpose?

It would have been just as easy to show off code that works across all generic sites, regardless of their domain name; but by instead using the code that you did, it makes it easier for inexperienced webmasters to accidentally forward their site over to yours.
Of course, I realize you'd never do anything like that. But it makes for a good conspiracy, don't you think? (c;

March 24, 2009 - 3:54am

If people are dumb enough to not test their redirects after setting them up, and then not notice they redirected their site to another, then they deserve to fail IMHO.

It is smart to use oneself as an example from a branding standpoint, which is why we did it here

wildinspire
April 14, 2009 - 6:41pm

I just migrated over my website from an old .asp site that used dynamic pages. I am now using a wordpress blog with market friendly URLs. I can get most of the straightforward redirects in the .htaccess file to work fine, but I'm running into the problem of linking the dynamic links. here are two separate examples below:

I need to redirect freetv.org/about/default.asp?ID=285 to freetv.org/about/board-of-directors/

or

freetv.org/about/default.asp?ID=282 needs to redirect to freetv.org/about/staff/

Can somebody help me with the redirect code for this?

I really appreciate any and all assistance!

mrealty
May 14, 2009 - 1:39am

I have a ton of urls that need to go 410 since gogle won't let us delete urls permanently from their index. If I have this:

/product-some-stuff-here-1402.html
/product-some-stuff-someother-random-stuff-1532.html

and I want to get rid of the whole range of urls ending in 1402 through 15323, how could I write this? It's just too much to put each and every last url in .htaccess. I figured if anyone would know - it would be you, after glancing at your "spammy with it".

With kind regards.

May 14, 2009 - 12:38pm

you can use wildcards in your .htaccess file, but unfortunately I have not yet done it and do not have an example bit of code to use.

jonray
June 1, 2009 - 1:40am

I have tried multiple versions of code in my .htaccess file and I continue to get this error:

Redirect Loop

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

Any ideas on getting this to work? My current .htaccess file looks like this:

# BEGIN WPSuperCache

RewriteEngine On
RewriteBase /
AddDefaultCharset UTF-8
RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz -f
RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz [L]

RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html -f
RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html [L]

# END WPSuperCache
# BEGIN WordPress

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
RewriteCond %{HTTP_HOST} ^makemoneyincollegeblog.com [NC]
RewriteRule ^(.*)$ http://www. makemoneyincollegeblog.com/$1 [L,R=301]

stepen
August 6, 2009 - 3:47pm

I'm new to .htaccess but all I know that to get correct result it should come to correct address.

gadget heaven
September 14, 2009 - 2:09pm

The good thing about the Internet is that there is so much good advice. The problem of course is trying to work out what's good and what isn't.

I have used the following in my .htaccess file ..

redirect 301 / http://www.newdomain.com/

However, whilst it works, I am getting 404 errors for pages and folders that 'were' on the old domain but are not on the new domain.

I want all pages from the old domain to point to the same page on the new domain. Is that possible?

I've tried using ..

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?(.*)$ http://www.newdomain.com/folder1/ [R=301,L]

But it gives a 403 error.

September 14, 2009 - 11:53pm

Yes it is possible to do a 301 redirect all pages on a site to 1 page on another site...but generally that is considered a poor SEO practice in most cases.

jehrenthal
September 17, 2009 - 1:40am

Ok, this sounds simple enough but even rereading the subject I am confused. So if I have example.com and I want to redirect the whole site to a new domain page to page i.e. example.com/page1 to newsite/page1 that is easy. But I need the home page from example.com to 301 to newsite.com/pag3, so while you covered the example in this post I don't know how to redirect the home page the .com page to a seperate page.

September 17, 2009 - 5:09am

redirect 301 /old-folder/oldpage.html http://www.example.com/new-folder/new-web-page.htm

redirect 301 /old-folder2/ http://www.example.com/page3

etc.

if you are doing it in bulk with patterns then it might make sense to use rewrite rules (rather than listing hundreds or thousands of redirect lines in your .htaccess file)

eDirectBiz
November 20, 2009 - 5:32pm

First off, this thread has been great! I've learned a lot.

I have a situation where I was asked to implement a 301 redirect for our entire site to a new site as we sold our site to another company. 3 months after doing this, management informs me that the sale has been canceled.

I still have control over the website and all of its assets. Can I reverse the 301 redirect in the .htaccess file? If so, what problems might this cause with the search engines?

November 20, 2009 - 7:13pm

That sounds like the type of question that would be good to ask in our member forums. I would hate to answer that publicly and then have others read the answer out of context and blame me for stuff that happened from that.

pmaonline
February 2, 2010 - 5:41pm

hi, i made a full reconstruction of my old site, but my url's have changed and now my links in google are not correct and i lost all rankings.

So my question is: is there any way to redirect the old urls in google to the new one's??

Thanks

colinthompson
April 19, 2011 - 9:06am

Hi, I'm moving a website and trying to replace aspx pages with html pages.

How do I configure .htaccess with mod rewrite to redirect pages like this

old page = mysite.com/contact.aspx
new page = mysite.com/contact.htm

Thanks

colinthompson
April 19, 2011 - 9:08am

site is being moved from Windows based hosting to Linux hosting

April 19, 2011 - 10:33pm
  • hiring a programmer who understands redirects
  • asking in a paid community like ours
  • if you want to do it for free then set up a second test domain & verify it works before doing it on your primary website

We can offer thousands of free in-depth blog posts with general-level advice, but it is the complete opposite of a business model for us to act as "the free programmer" or some such & provide 1:1 support without getting paid for it. Worse yet, if something didn't work because your server had some sort of weird configuration the person spending their resources giving you advice could get blamed for crashing the site or server.

.htaccess files are hit or miss, and the misses can be quite brutal ;)

Serperb
June 17, 2011 - 12:12am

For anybody having trouble with 500 http status error codes, try changing the encoding of the .htaccess to ANSI. Worked for me.

johnashmit
March 21, 2012 - 1:03pm

suppose for a while i have created i have redirected my website from non www to www site and still there is a room to redirect my index.html to FQDN.
For this would i have to create an another .htaccess file or i have to do some another entry in the same file?
Kindly suggest?

John Doe
February 7, 2014 - 12:28pm

Hello,

I read your article and it seems that there is one failure in description of the first code?

CODE:
RewriteRule ^(.*)$ http://www.seobook.com/$1 [L,R=301]

Description:
The '(*.)$' says that we'll take anything that comes after http://seobook.com and

It should be "The '(.*)$' says that ...anything that comes after..... isn`t it?

cheers John

surafel tefera
November 14, 2018 - 7:26pm

What if you want to use this

RewriteRule ^superhotproduct(.*)$ /new-hot-stuff.php [L,R=301]

to redirect to more than one file?

January 2, 2020 - 2:24pm

...any specific URL can only be redirected to a single location (unless you were doing some sort of A/B split test through an intermediary page which then used PHP or such to redirect for testing purposes, though most would recommend keeping the URL the exact same and using JavaScript to test altering the on-page content)

if you have a catch-all redirect which redirects most pages from a folder to a destination and you have a more-specific alternate location for other redirects you can put those alternative redirects higher up in the .htaccess file.

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.