htaccess Tips and Tricks
Last Updated: May 24, 2020
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.
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:
- Six valuable .htaccess tips
- Excel for SEO – Powerful Cheat Sheet to Boost Productivity
- How to do Site Speed Optimisation
- Ultimate Data Visualization Guide for SEO
- How to write an SEO Contract
- How to Automate Event Tracking in Google Analytics
- Social interactions tracking through Google Analytics
- Regular Expression Guide for SEOs
- SEO Contract | Sample SEO Contract Template
- Event Tracking – Google Analytics (Simplified Version)
Last Updated: May 24, 2020
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.
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:
- Six valuable .htaccess tips
- Excel for SEO – Powerful Cheat Sheet to Boost Productivity
- How to do Site Speed Optimisation
- Ultimate Data Visualization Guide for SEO
- How to write an SEO Contract
- How to Automate Event Tracking in Google Analytics
- Social interactions tracking through Google Analytics
- Regular Expression Guide for SEOs
- SEO Contract | Sample SEO Contract Template
- Event Tracking – Google Analytics (Simplified Version)
Register for the FREE TRAINING...
"How to use Digital Analytics to generate floods of new Sales and Customers without spending years figuring everything out on your own."
Here’s what we’re going to cover in this training…
#1 Why digital analytics is the key to online business success.
#2 The number 1 reason why most marketers are not able to scale their advertising and maximize sales.
#3 Why Google and Facebook ads don’t work for most businesses & how to make them work.
#4 Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.
#5 The number 1 reason why conversion optimization is not working for your business.
#6 How to advertise on any marketing platform for FREE with an unlimited budget.
#7 How to learn and master digital analytics and conversion optimization in record time.
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.