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:

Announcement about my books

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

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 eleven 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 three books:

error: Content is protected !!