How to Redirect a folder to a Subdomain using .htaccess

January 7th, 2010

If you create a sub domain (e.g. shop.example.com) from cPanel you will find that a subfolder (example.com/shop) in the same name has been added to the directory list. When you upload anything on the folder, it will reflect in the subdomain created. When you visit shop.example.com or example.com/shop, you will see that the content is the same.

There is nothing wrong with it, however from the view point of SEO and Search engines, this creates duplicate content, because… for search engines a folder is different from a subdomain. To avoid this issue we can simply add or edit the .htaccess. This will tell SE’s that the folder has been permanently redirected to the sub domain.

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

Please change (subdomain, example) as per your website.

Editing .htaccess from cPanel causing website errors?

October 27th, 2009

Sometimes when you edit the .htaccess file in cPanel, the lines must wrap and this causes a line break.

The line break is a common reason for site errors.

1. To fix this, open a text editor (like Notepad) and paste your .htaccess code. Be sure that Word Wrap is off (under Format).
2. Save the file as htaccess.txt
3. Upload this new file to the proper directory.
4. Rename the current .htaccess to htaccess-old.txt
5. Rename htaccess.txt to .htaccess

To avoid further problems, don’t edit .htaccess in cPanel anymore use FTP instead.

How to run your static HTML pages as PHP using .htaccess

October 25th, 2009

Apache handlers allow you to control what Apache will do with certain file types. When Apache sees a file, it has an action built in for that file type, and will perform that action.

If you wish Apache to do a different action, you will need to make a handler to tell Apache to perform that action. For example, if you use a file type that requires a special service to run it, such as a file with server side includes that is not named with a .shtml extension, you need to tell Apache to treat these files differently.

To get regular html pages to handle php code, you need to add this line to your htaccess file.

AddHandler application/x-httpd-php5 .html .htm

Note:It is highly recommended that you never allow html pages to automatically handle php or shtml, because this forces all of your html pages to be processed by the server first. Instead, please rename your files to .php or .shtml whenever possible.

Creating 301 Permanent Redirect

August 16th, 2009

301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. It’s not that hard to implement and it should preserve your search engine rankings for that particular page (including Google PR). If you have to change file names or move pages around, it’s the safest option. The code “301” is interpreted as “moved permanently”.

1. Login to cPanel and click the Redirects icon.
2. Leave the setting as 301 redirect, as this is preferred by search engines.
3. Select the appropriate domain name from the drop-down.
4. In the subsequent blank, enter an optional path and file name; this exact name must be typed to trigger the redirect.
5. In the large blank, enter the full URL for the redirect target; include http:// or https:// (e.g. http://speedhost.in)
6. Leave the other settings as they are, unless you know why you want to change them.
7. Click the Add button and you are done.

The above would make an entry directly to your .htaccess file. If you would like to do a 301 redirect in the .php file itself, then you can use the below code to accomplish the same.

Header( "HTTP/1.1 301 Moved Permanently" ); 
Header( "Location: http://www.new-url.com" );