website redirection using .htaccess file

I have added in this article for all the possibile examples for .htaccess redirection. I can DAM sure, definitely you will get the redirection script from any of the below examples. Simple you can add the following code into your .htaccess file to do your redirection. You can find the .htaccess file into your website root directory and if you don’t find it, you can copy this into a text file and save it as .htaccess, then you can upload it.

1) from www to Non-www redirection

I have added here seven examples script for www to Non-www redirection.

# Example - 1 #
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.2daygeek\.com [NC]
RewriteRule ^(.*)$ https://2daygeek.com/$1 [L,R=301]

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

# Example - 3 #
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R]

# Example - 4 #
RewriteEngine On
RewriteCond %{HTTP_HOST} !^2daygeek\.com
RewriteRule (.*) https://2daygeek.com/$1 [R=301,L]

# Example - 5 #
RewriteEngine On
RewriteCond %{HTTP_HOST} !^2daygeek\.com$ [NC]
RewriteRule ^(.*)$ https://2daygeek.com/$1 [R=301,L]

# Example - 6 #
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.2daygeek\.com [NC]
RewriteRule (.*) https://2daygeek.com/$1 [R=301,L] 

# Example - 7 #
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?2daygeek\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

2) from Non-www to www redirection

I have added here five examples script for www to Non-www redirection.

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

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

# Example - 3 #
RewriteEngine on
RewriteCond %{HTTP_HOST} ^2daygeek\.com [NC]
RewriteRule (.*) https://www.2daygeek.com/$1 [R=301,L] 

# Example - 4 #
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.2daygeek\.com
RewriteRule (.*) https://www.2daygeek.com/$1 [R=301,L] 

# Example - 5 #
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?2daygeek\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]

3) https redirection

For https redirection form www to Non-www and Non-www to www add, s to any of the above examples to trick it.

4) For sub-folder redirection

For sub folder redirection. Like if you have redeveloped your website and the contents are stored at “/demo’ folder and if you want that to be redirected to the main domain in sub folder, then try any of the below examples.

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

# Example - 2 #
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(www\.)?2daygeek\.com$ [NC] 
RewriteRule ^$ http://%{HTTP_HOST}/demo/index.php [R,L]

# Example - 3 #
RewriteEngine On
RewriteRule ^$ /demo [L]

# Example - 4 #
RewriteEngine on
RewriteCond %{HTTP_HOST} ^2daygeek\.com$
RewriteRule (.*) https://www.2daygeek.com/$1 [R=301,L]
RewriteRule ^$ demo [L]
or
RewriteRule ^$ /demo [L,R=301]

# Example - 5 #
RewriteEngine On
RewriteRule ^(.*)$ /demo/$1 [L]

# Example - 6 #
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) https://www.2daygeek.com/demo [R=301,L]

# Example - 7 #
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?2daygeek.com$
RewriteRule ^(/)?$ demo [L]

# Example - 8 #
RewriteEngine On
RewriteRule !^demo/ /demo%{REQUEST_URI} [L,R=301]

# Example - 9 #
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?2daygeek.com$
RewriteCond %{REQUEST_URI} !^/demo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /demo/$1
RewriteCond %{HTTP_HOST} ^(www.)?2daygeek.com$
RewriteRule ^(/)?$ demo/index.php [L]

# Example - 10 #
Options -Indexes +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/demo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /demo/$1 [L]

# Example - 11 #
Options -Indexes +SymLinksIfOwnerMatch 
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/demo/
RewriteCond %{HTTP_HOST} ^(www\.)?2daygeek\.
RewriteRule ^(.*)$ /demo/$1 [L]

Hope this article was useful for you. Kindly provide your valuable feedback in the commenting section. Stay tuned with us !!

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

One Comment on “website redirection using .htaccess file”

Leave a Reply

Your email address will not be published. Required fields are marked *