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:

 

How to use Digital Analytics to generate floods of new sales and customers for your business

(Even if you are completely new to Digital Analytics)

Here’s What We’re Going To Cover…

#1 - The number 1 reason why most marketers and business owners are not able to scale their advertising and maximise sales. 

#2 - Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics. 

#3 - The number 1 reason why conversion optimization is not working for your business. 

#4 - How to advertise on any marketing platform for FREE with an unlimited budget.

 

Register for free training

 

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 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.

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.

Himanshu Sharma

Digital Marketing Consultant and Founder of Optimizesmart.com

Himanshu helps business owners and marketing professionals in generating more sales and ROI by fixing their website tracking issues, helping them understand their true customers purchase journey and helping them determine the most effective marketing channels for investment.

He has over 12 years experience in digital analytics and digital marketing.

He was nominated for the Digital Analytics Association's Awards for Excellence.

The Digital Analytics Association is a world renowned not-for-profit association which helps organisations overcome the challenges of data acquisition and application.

He is the author of four best-selling books on analytics and conversion optimization:

error: Alert: Content is protected !!