Pages

martes, 7 de enero de 2014

How To Redirect Your Domain To A Subfolder Using .htaccess

Sometimes the simple things are the hardest to figure out and today we are going to show you a simple way to redirect your primary domain to a subfolder on that domain. For the purposes of this tutorial it will redirect to a subfolder at /blog. This is useful if your WordPress blog is installed to a subfolder and you want the main domain to forward to it.
In your public_html folder (also called the "root html" folder), you need to create a file called .htaccess. From this file we are going to use a few lines to redirect those requests in a search engine friendly way.
Add the following lines to that file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
RewriteRule ^(/)?$ blog [L]
The only change you need to make is to change "YourDomain.com" to your domain. This will then redirect everything from the domain to the folder /blog at that domain. If you want to change that folder just edit the "blog" part.
And you are good to go! For further information on what you can do with a .htaccess file I recommend the following sites…
  • Comprehensive Guide To .htaccess – Scroll to the bottom of the page for 12 tips on things you can do with the .htaccess file. Everything from password protection to redirects to preventing directory listings.
  • .htaccess Cheat Sheet – A nice listing of different tips and tricks to use. Sparse on explanations though (that is why it is a cheat sheet).
  • .htaccess Tools – A great site that has some basic tools online that can generate everything for you. Nice article on how to detect and redirect iPhone users as well.
  • Perishable Press Stuipd .htaccess Tricks – When you finish reading this you will be a .htaccess ninja! 56 pages of easy to read documentation. At least easier to read than the official Apache docs.
Just a word of warning, Apache redirects are often a thing of voodoo magic and for really complex rules you might have to play with them for a while to get them to work how you want.

No hay comentarios:

Publicar un comentario