Redirecting from a WWW Domain to a non-WWW Domain
# 301 redirect(permanent redirect) from http://www.sample.com to http://sample.com RewriteEngine on Options +FollowSymLinks RewriteCond %{HTTP_HOST} ^www.sample.tld$ [NC] RewriteRule ^(.*)$ http://sample.com/$1 [R=301,L]
Redirecting from One Domain to Another Domain
Suppose we want to redirect all the pages & posts of http://www.sample1.com
to http://www.sample2.com
. To do this, you just have to place the below code in .htaccess file of http://www.sample1.com
.
# redirect from http://www.sample1.com to http://www.sample2.com RewriteEngine On RewriteRule ^(.*)$ http://www.sample2.com/$1 [R=301,L]
Redirect 301
301 redirect is also known as permanent redirect. It redirects visitors to new page as well as it signals search engine bots that the page has been moved permanently to a new location.
Redirecting a page to a different location –
Here I am redirecting URL http://sample.com/category1/page1.html
to http://sample.com/category2/page2.html
# 301 Redirect example Redirect 301 /category1/page1.html http://sample.com/category2/page2.html
It can also be written as –
Redirect permanent /category1/page1.html http://sample.com/category2/page2.html
In the above example we are setting up a redirection within the same site. If you are planning to setup a redirect between two different websites then RewriteRule is a better option.
Example –
I’m using the same example of sample1 and sample2
# RewriteRule RewriteRule http://www.sample1.com/firstpage.php http://www.sample2.com/secondpage.php RewriteRule http://www.sample1.com/category/ http://www.sample2.com/mynewcat/ RewriteRule http://www.sample1.com/ http://www.sample2.com/
Redirect whole website to a new Domain’s homepage
Say, the below code is placed in sample1’s .htaccess file, then any requested url for sample1 would be redirected to the sample2’s home page (i.e. http://www.sample2.com/
)
# permanent redirect for a whole site Redirect 301 / http://www.sample2.com
The above code can also be written as –
Redirect permanent / http://www.sample2.com
Redirect if File doesn’t exist
If my file – http://sample.com/category/page.php doesn’t exist then redirect to http://sample.com/category2/subcat/. Lets write it in .htaccess file –
# rewrite only if the file doesn't exist RewriteCond %{REQUEST_URI} ^/category/(.*)/page.php$ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^category/(.*)/page.php$ category2/subcat/$1
Redirect to SubDomain
For example, I want to redirect my visitors to http://mysub.sample.com
from http://sample.com
then you can write it as-
# send visitors to a subdomain RewriteCond %{HTTP_HOST} !^$ RewriteCond %{HTTP_HOST} !^subdomain.domain.com$ [NC] RewriteRule ^/(.*)$ http://mysub.sample.com/$1 [L,R=301]
Fix duplicate content issue using rewrite
For example – my website www.sample.com has a webpage which would be displayed when user enters any of the below url in browser –
http://www.sample.com/mypage
http://www.sample.com/mypage?id=94
If you consider the SEO then these pages may be treated as duplicate pages so to avoid such situation you can redirect the pages of second type to the first one.
RewriteCond %{THE_REQUEST} ^GET /.*;.* HTTP/ RewriteCond %{QUERY_STRING} !^$ RewriteRule .* http://www.sample.com%{REQUEST_URI}? [R=301,L]
After having the above code in .htaccess, when user enters the url – http://www.sample.com/mypage?id=94
, it would be redirected to the – http://www.sample.com/mypage
Leave a Reply