htaccess Tips and Tricks

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   https://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.

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

Another example:

Redirect 301  /what-is-event.html  https://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

Converting dynamic URLs into static looking SEO friendly URLs

How to redirect:
https://www.example.com/productdescription.php?keyval=25&keyval2=62
to
https://www.example.com/whiteboard-accessories.php

Add the following code in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteCond   % {QUERY_STRING}   ^keyval\=25\&Keyval2\=62$ [nc]
RewriteRule   ^productdescription.php$  https://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.

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?$ https://www.mywebsite.com/$1 [R=301,L]

Step 2: 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  ^(.*)$  https://www.mywebsite.com/$1 [r=301,nc]

Step 2: Replace ‘mywebsite’ by your website name

How to create custom 404 page

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

Step 2:  Upload your webpage to the root directory.

Step 3:  Add following code to your .htaccess file:

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

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

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.

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

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

Step 2: Replace ‘mywebsite’ by your website name.

Step 3: 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:

Take the Course

Most Popular E-Books from OptimizeSmart

Learn to read e-commerce reports book banner

Check out my best selling books on Web Analytics and Conversion Optimization on Amazon

How to get lot more useful information?

I share lot more useful information on Web Analytics and Google Analytics on LinkedIn then I can via any other medium. So there is really an incentive for you, to follow me there.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over twelve years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of four books:

error: Alert: Content is protected !!