htaccess redirecting basics
This is going to be a guide for begginers, so don’t take it personal if I go into dumb details. I am assuming that you have a basic LAMP configuration (Linux, Apache, MySQL, PHP) with a web administration interface such as cPanel.
All the following scenarios will require you to edit the .htaccess file in your root directory. Htaccess is an optional configuration file used by Apache to handle all incoming requests. You will also need support for mod_rewrite.
Redirecting example.com to www.example.com
The reason for doing this is because, by default, you will have 2 distinct URLs for each page on your website. For example http://www.example.com/download/games.html and http://example.com/download/games.html are one and the same page. Unfortunately search engines assume that they are completely distinct. Some say this will trigger the so-called duplicated content filter or that pagerank may spread across too many pages.
The solution is to redirect one url to the other with the response code 301. I will just use the non-www to www redirection in this article. You need a rule that will be applyable to all the URLs on your website.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This code will find all requests that contain the host example.com and redirect them using a 301 code to www.example.com. The best part is that any string after the host is taken care of. In other words, http://example.com/page1.html will be redirected to http://www.example.com/page1.html.
Redirecting a subfolder to the subdomain
When you add a new subdomain in cPanel, a folder with the same name is created in your root directory. This is where all the files directly accessible from the subdomain are located. The downside is that you now have 2 ways of viewing the same file: http://www.example.com/subdomain/page.html and http://subdomain.example.com/page.html. This is not as crucial as the scenario previously discussed because the chances of someone linking to your “subdomain” folder are pretty slim. However, we are really into eliminating every duplicated URL out there so we will deal with this problem too :).
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain(.*)$ [NC]
RewriteRule ^subdomain/(.*)$ http://subdomain.example.com/$1 [L,R=301]
Having these lines in .htaccess will make sure that http://www.example.com/subdomain/page.html will redirect to http://subdomain.example.com/page.html. But we still have http://example.com/subdomain/page.html which is not redirected. So what now?
Combine them both!
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain(.*)$ [NC]
RewriteRule ^subdomain/(.*)$ http://subdomain.example.com/$1 [L,R=301]
You are now fully covered!
- http://example.com redirects to http://www.example.com
- http://example.com/somepage.html redirects to http://www.example.com/somepage.html
- http://www.example.com/subdomain/ redirects to http://subdomain.example.com/
- http://www.example.com/subdomain/somepage.html redirects to http://subdomain.example.com/somepage.html
- http://example.com/subdomain/somepage.html redirects to http://www.example.com/subdomain/somepage.html which redirects to http://subdomain.example.com/somepage.html
7 Comments to “htaccess redirecting basics”
Leave a Reply

I am having problem redirecting blog.domain.com to http://www.domain.com/blog/
I used the following code…
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.domain\.com
RewriteRule ^(.*)$ /blog/$1 [L,R=301]
but it redirects to http://www.domain.com without the directory /blog/
Also tried other “htaccess redirect subdomain folder” examples around the web but with the same effect… none seems to be working… any ideea how to make this work?
Try this out:
RewriteEngine onRewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/blog/$1 [L,R=301]
I haven’t tested it yet so let me know how it goes.
The above code should redirect blog.example.com/anything.html to http://www.example.com/blog/anything.html
Nope… still not working properly… it redirects to http://www.example.com
when accessing an old blog post from blog.domain.com it gives an Internal Server Error 500
OK, I’ve done some testing:
check http://test.ylipsis.com/somedirectory/anything.html?varname=varvalue
It redirects exactly as you wanted (passing the subdomain to the directory)
And here is the code in my htaccess (the one in public_html aka root directory):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.ylipsis\.com$ [NC]
RewriteRule ^(.*)$ http://www.ylipsis.com/$1 [L,R=301]
* there is a dollar sign on the last line – c o m / $ 1. This seems to get parsed by wordpress…
I have One issue, please help me. My issue is I have url http://subdomain.mydomain.com and want to redirect them http://mydomin.com/folder1/index.php?user=subdomain. but I dont want to change URL in addressbar so in addressbar the url should be http://subdomain.mydomain.com. What i have to do? My .htaccess code is
=================================
#RewriteCond %{HTTP_HOST} !^www\.mydomain.com [NC]
#RewriteCond %{HTTP_HOST} ([^.]+)\.mydomain.com [NC]
#RewriteRule ^(.*)$ http://mydomain.com/folder1/index.php?user=%1 [L]
=================================
It redirect correctly, But Unfortunitly, it affect addressbar, so it’s useless for me. Please help me. What I have to do? Thanks in advance.
I don’t know how to do that. But you may try a different approach:
1) Add the subdomain *. This ensures that http://whateveryoutype.example.com will show the content of http://www.example.com
2) Modify the index.php file so that the first thing it does is checking the HTTP_HOST. If it is anything else than http://www.example.com than loads the folder1/index.php file.