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 a question mark (?) at the end of the substitution URL, otherwise query string will be appended at the end of the substitution URL.

Do you want expert help in setting up/fixing GA4 and GTM?

If you are not sure whether your GA4 property is setup correctly or you want expert help migrating to GA4 then contact us. We can fix your website tracking issues.

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 the 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 the 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 hotlinking Issue

Hotlinking means direct linking to your website file (images, videos, etc). By preventing hotlinking, you can save your server 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 a hotlinking checker tool to find out whether your files (images, videos, etc ) can be hotlinked 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.

Other posts you may find useful:

My best selling books on Digital Analytics and Conversion Optimization

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 BeyondSECOND EDITION OUT NOW!
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.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade