Linux/htaccess

= Redirect pages using htaccess file =
 * source: http://www.htaccessredirect.co.uk/

htaccess Redirect

 * How to redirect webpages


 * The .htaccess file is a small text document that generally sits in the same location as your index.php or index.htm pages. It gives you the ability to interact with Apache on an individual domain-to-domain and directory-to-directory basis.


 * You can place the htaccess file anywhere where you'd like to control the flow of visitors. So for instance you can protect directories and redirect the traffic visiting those pages. This page will show you how to use the .htaccess file to redirect your visitors in different ways. Note that this is a powerful system file, if the syntax is incorrect in anyway, it could render your site unusable. Always take a backup.


 * Check out the | 301 redirect tutorial at webconfs.com for more information on creating search engine friendly redirects

Redirect to another page

 * To redirect one page to another page:

Redirect /old-index.html http://www.mynewwebsite.com/foldername/new-index.html

Redirect to another website

 * To redirect an entire website from one url to another:

Redirect 301 / http://www.mynewwebsite.com

Redirect index to subdirectory

 * To redirect a page to a subdirectory:

Redirect /index.html http://www.mynewwebsite.com/foldername

Redirect filepaths

 * To redirect a filepath to another filepath:

Redirect /foldername/filename.html http://www.mynewwebsite.com/foldername2/filename.html

To change file extention

 * If you've changed your pages from .html to .php:

RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php

Specifying folder default page

 * To change the default webpage loaded by the server:

DirectoryIndex index.php

Redirect www to non-www

 * To redirect http://www.mysite.com to http://mysite.com:

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

Redirect non-www to www

 * To redirect http://mysite.com to http://www.mysite.com:

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

Give 307 'Site Under Maintenance' Header on all webpage requests

 * To return a 307 Site Under Maintenance header to those visiting the site. Create a temporary file called 307.php and inside it place the message to give to your visitors and upload it to your public root directory.
 * Next create a totally separate .htaccess file called .htaccess.307 with the following text:

RewriteEngine On

RewriteBase /


 * 1) Before using this htaccess, you have to change this digits to match your


 * 1) own IP address: This will keep you with access to the site. as long as


 * 1) your IP doesn't change: http://whatismyip.com

RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123$


 * 1) The last 2 lines take the site offline. the basically say if: page request is


 * 1) NOT 307.php, show 307.php but only once. Stops it from looping endlessly.

RewriteCond %{REQUEST_URI} !^/307\.php$


 * 1) The browser gets this bit, so you need the full website address.

RewriteRule ^(.*)$ http://www.yoursite.com/307.php [R=307,L]


 * The above includes comments for your convenience. To get the redirect up and running, rename your existing .htaccess file to live.htaccess then rename this one to .htaccess

Alternative Redirect 1: Meta Refresh

 * It's also possible to redirect traffic when visiting a page with the following line inside the head tag.
 * To do an immediate redirect:


 * To redirect after 5 seconds:



Alternative Redirect 2: PHP Header Redirect

 * You're able to redirect traffic by putting the following line at the very top of a php document (nothing can be above it).

