Htaccess Redirects

May 14th, 2008 | By admin | Category: Linux

Web site creating is making directories and files connected in someway. If you even have fewer web sites, you probably came across with the situations you wanted to redirect your visitors to someother pages. You might move your domain to a new one, change your file’s extensions or change your subfolder name.

There are different methods for different kinds of problems. If you are looking for htaccess solutions, here is the methods:

If you don’t have a “.htaccess” file (a hidden file) on your server, create one and upload it in ASCII mode

Simple Page Redirect:
The following code will redirect your visitors from example.html to example2.html page.

Redirect 301 /example.html http://www.domain.com/example2.html

301 means you redirected the page permanently. If it is a temporary redirect, remove that 301 from the code.

Simple Folder Redirect:
The following code will redirect all the visitors to another folder.

Redirect 301 /shop https://www.domain.com/shop

Redirect to Matched Page/Folder:

If you want to redirect your old domain to a new domain with all the files and subfolders, the following code will redirect a visitor (or search engine) to the same file at the new domain.

RedirectMatch 301 ^(.*)$ http://www.newdomain.com/

Alternatively you can use this:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

This code will redirect all the visitors from olddomain.com/whatever to newdomain.com/whatever so there will be no need to redirect all the pages individually.

Redirect to New File Extension

If you changed some file extensions and want to redirect your visitors to new extensions, you may want to add the following code to your htaccess:

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

This code will redirect all the html files to php extension pages. Meaning your visitors will be redirected to domain.com/home.php instead of domain.com/home.html.

Redirect Your Whole Root Folder to a SubFolder

Say you have your website under a folder named “subfolder”, and you want to access those files from your root folder as well. For example you want to access “domain.com/subfolder/page.html” from “domain.com/page.html”. The following code will do the trick.

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} domain.com
RewriteCond %{REQUEST_URI} !subfolder/
RewriteRule ^(.*)$ subfolder/$1 [L]

Redirect www to non www version of site

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

Redirect non-www to www

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

  • Share/Save/Bookmark
Tags:

2 comments
Leave a comment »

  1. [...] Htaccess Redirects Web site creating is making directories and files connected in someway. If you even have fewer web sites, you probably came across with the situations you wanted to redirect your visitors to someother pages. You might move your domain to a new one, change your file’s extensions or change your subfolder name. [...]

  2. Thanks for the information, I was wondering if you know how to redirect traffic going to a domain, into the parent folder….eg:

    I want the domain pointing to /home/xxxxxxxx/public_html/mydomain.com/ to rather go to /home/xxxxxxxx/public_html/ and open the website there instead.

    Is something like that possible?

Leave Comment