htaccess tips and tricks

1. How to apply 301 from one file to another file

Step 1: Add following code in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on
Redirect 301  /file1.html   http://www.mywebsite.com/file2.html

The above code will permanently redirect file1.html to file2.html. So whenever a search engine or a visitor will look for file1.html, he will automatically be redirected to file2.html.

Step2: Replace ‘mywebsite’ by your website name and file1.html and file2.html by your file names.

Another example:

Redirect 301  /what-is-event.html  http://www.eventeducation.com/what-is-event.php

Note: If the following lines are already there in your .htaccess file, then don’t add them again:

Options +FollowSymLinks
RewriteEngine On

………………………………….

2. Converting  Dynamic URL into Static Looking  SEO friendly URL

How you can redirect:
http://www.example.com/productdescription.php?keyval=25&keyval2=62

to
http://www.example.com/whiteboard-accessories.php

Add following code in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on

RewriteCond   % {QUERY_STRING}   ^keyval\=25\&Keyval2\=62$ [nc]
RewriteRule   ^productdescription.php$  http://www.example.com/whiteboard-accessories.php? [r=301, l]

Note: You need to put question mark (?) at the end of the substitution URL, otherwise query string will be appended at the end of the substitution URL.

…………………………………….

3. How to redirect index.php to the root

Step 1: Add following code in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{THE_REQUEST} ^.*\/index\.phpl?
RewriteRule ^(.*)index\.php?$ http://www.mywebsite.com/$1 [R=301,L]

Step2: Replace ‘mywebsite’ by your website name

—————————————————————–

How to redirect non-www to www using mod_rewrite module

Step 1: Add following code in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on
rewritecond  %{http_host}  ^mywebsite.com [nc]
rewriterule  ^(.*)$  http://www.mywebsite.com/$1 [r=301,nc]

Step2: Replace ‘mywebsite’ by your website name

 

……………………………………..

4. How to create Custom 404 page

Step1: Create a web page which you want to display as your custom 404 page say custom404.php

Step2:  Upload your webpage to the root directory.

Step3:  Add following code to your .htaccess file:

Options +FollowSymLinks
RewriteEngine on
ErrorDocument  404  http://www.mywebsite.com/custom404.php

……………………………………..

5. How to block an IP address from accessing your website

Add following code in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on
Order  Deny, Allow
Deny from  61.16.153.67

If you want to block two or more IP addresses

Options +FollowSymLinks
RewriteEngine on

Order  Deny, Allow
Deny from  61.16.153.67
Deny from  124.202.86.42

………………………………………….

6. How to resolve the Hot Linking Issue

Hot-linking means direct linking to your website file (images, videos etc). By preventing hot-linking, you can save your sever bandwidth.

Step1: Add following code in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mywebsite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpg|jpeg|gif|bmp|png|swf)$ – [F]

Step2: Replace ‘mywebsite’ by your website name.

 

Step3: Use hotlinking checker tool to find out whether your files (images,videos etc ) can be hot-linked or not.

 

Note: Don’t copy the code from this blog post straight to your .htaccess file. First copy it to the text file to remove the formatting. Otherwise the code may not work.

 

If you like this post then you should subscribe to my blog and follow me on twitter.

 

Other Posts you may find useful:

 

Himanshu Sharma

About the Author: is the founder of seotakeaways.com which provides SEO Consultation, PPC Management and Analytics Consultation services to businesses of all size. He holds a bachelors degree in ‘Computer Science’, is a proud member of 'Digital Analytics Association' and is also a Google Analytics Certified Individual with GAIQ Score of 95%. He is also the founder of EventEducation.com and EventPlanningForum.net.

 

 

5 Responses to htaccess tips and tricks

  1. nice article. How i can protect my wordpress blog from hotlinking?

    • seo himanshu says:

      Go to the file manager of your hosting account, edit your .htaccess file and place the hotlinking code there.

  2. Robert says:

    thank you for this useful tips. saved in my favorites.

  3. nice collection of tips. I am going to implement few of them.

  4. thank you very much Himanshu Sharma.

Leave a Reply

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

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>