Permanent 301 Redirect the easy way

First, .htaccess and mod_rewrite can be very tricky and if you break your website redirect you may even fix it and not know because your browser session has not been restarted.

Use [R] instead of [R=301] while you are testing . When you are 100% certain the rule does exactly as it’s expected to, then switch it to [R=301] for your live site.

How to redirect any and all website pages to another website removing the page reference. This way if you have an old website with old links everyone will be redirected to the front door. Search engines will see the permanent 301 redirect and update their index to not list your old website.

# redirect anything with base url in the second line to the first one and remove any page references
redirectMatch 301 ^(.*)$ http://mynewwebsite.com
redirectMatch permanent ^(.*)$ http://www.myoldwebsite.net

Redirect with URL masking:
from: http://answers.yahoo.com/question/index?qid=20090904141904AAd3mjB
“the apache rewriteengine doesn’t have any functionalities to mask URLs. Traditional masking techniques (pull out the source of the page that’s been masked and you’ll see) is done through a frame.


However you can accomplish dynamic masking by a little bit of additional code (I have an example in django but it’s out of the scope of this discussion.)

Here’s a basic masking frame

<frameset rows = “100%, *” border = “0” framespacing=”0″ frameborder=”0″>

<frame noresize = “” src = “http://yourwebsite.com/”></frame>

</frameset>

@EDIT:

That would be for contemporary redirects.

Let’s say that you want host1.com to redirect to mysite.com

The usual .htaccess redirect rule would be

RewriteEngine on
RewriteCond %{HTTP_HOST} ^host1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.host1.com$
RewriteRule ^.*$ “http://mysite.com/” [R=301,L]

in apache.

(The string of ” acts as escape sequences for [‘:’,’/’,’.’], with a R response code of 301)

However this does not allow you to mask the pages.

In order to achieve masking effects, use the following as your HTML in the index.html file on host1.com

<frameset rows = “100%, *” border = “0” framespacing=”0″ frameborder=”0″>

<frame noresize = “” src = “http://mysite.com/”></frame>

</frameset>

Just copy paste this into the index.html file under host1.com and it should work.”

More .htaccess tricks with various redirects and how to do them:

from: http://corz.org/serv/tricks/htaccess2.php

Leave a comment