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

Jun 7th

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).

RewriteEngine On

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

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.

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

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

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:

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

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:

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

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

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

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:

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

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:

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

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

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;
$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"];
return $ret;

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

header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");

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


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?

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?

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?

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]

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?

November 20, 2006 - 11:44pm

What I'm trying to do is 301:




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?



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

New url is

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') {
if (isset($_SERVER['QUERY_STRING'])&&($_SERVER['QUERY_STRING']!=''))
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$nohttps);

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?


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.


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.

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,


/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.

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 ;-)

June 14, 2007 - 6:31pm


I would like to permanently move

to this


to this

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

June 8, 2006 - 4:59am


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...


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

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:

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.


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.





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

must redirects to

must redirects to

must redirects to

must redirects to


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

must redirect to

must redirect to


must redirect to


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

must redirect to

must redirect to


must redirect to



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.

September 1, 2006 - 12:50am

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

For example, I have:

How do the bots know that the options are not

(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

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

July 7, 2006 - 10:06pm


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


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.


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


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}


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

November 21, 2007 - 8:46pm

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

is redirected to

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?

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.

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. :)

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.

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.

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]

October 23, 2008 - 8:24am

Lot of thanks.

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

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/


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!

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:


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.

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 %{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 %{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]

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.

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.

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


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)

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.

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??


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


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 ;)

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.

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


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

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

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

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.

Gain a Competitive Advantage Today

Your top competitors have been investing into their marketing strategy for years.

Now you can know exactly where they rank, pick off their best keywords, and track new opportunities as they emerge.

Explore the ranking profile of your competitors in Google and Bing today using SEMrush.

Enter a competing URL below to quickly gain access to their organic & paid search performance history - for free.

See where they rank & beat them!

  • Comprehensive competitive data: research performance across organic search, AdWords, Bing ads, video, display ads, and more.
  • Compare Across Channels: use someone's AdWords strategy to drive your SEO growth, or use their SEO strategy to invest in paid search.
  • Global footprint: Tracks Google results for 120+ million keywords in many languages across 28 markets
  • Historical data: since 2009, before Panda and Penguin existed, so you can look for historical penalties and other potential ranking issues.
  • Risk-free: Free trial & low price.
Your competitors, are researching your site

Find New Opportunities Today

    Email Address
    Pick a Username
    Yes, please send me "7 Days to SEO Success" mini-course (a $57 value) for free.

    Learn More

    We value your privacy. We will not rent or sell your email address.