10 SEO Tips For .htaccess file snippet

  • December 12, 2012
  • News

Make your .htaccess file more powerful with the useful SEO tips which can bring drastic changes in your SEO strategies. If there is anyone who are not aware about .htaccess file, the htaccess file is a type of config file for the Apache server which allows you to manipulate and redirect URLs amongst other things. All the tips are quite useful but specifically number 4 which is about the redirection which will be loved by SEO specialist and serve them in good manner.  The small changes can bring big results. Apply the tips in your .htaccess file and see the results.

1.      Make URLs SEO-friendly and future-proof
A SEO friendly URL which allows you to better understand the structure of the file and allow you signifies the products with the help of slashes. Apache identifies files and how to handle them by their extensions, which we can override on a file by file basis:
<Files magic>
ForceType application/x-httpd-php5
This will allow the ‘magic’ file, which is a PHP file without an extension, to then look like a folder and handle the ‘inner’ folders as parameters. You can test it out here (try changing the folder names inside the magic ‘folder’

2.      Apply rel=”canonical” to PDFs and images
One of the new ways to attach images to the content is by applying rel=”canonical” to PDF’s. It is one of the easiest tags of HTML which comes under head section. However, many people still don’t know that you can apply rel=”canonical” in an alternative way, using HTTP, for cases where there is no HTML to insert a tag into. An often cited example that can be used for applying rel=”canonical” to PDFs is to point to an HTML version or to the download page for a PDF document. An alternative use would be for applying rel=”canonical” to image files. It is much useful as rel=”canonical” links to images to the product page are able to connect user to the product page and proved much reasonable.
Here is an example of applying HTTP rel=”canonical” to a PDF and a JPG file:
<Files download.pdf>
Header add Link ‘<http://www.tomanthony.co.uk/httest/pdf-download.html>; rel=”canonical”‘
<Files product.jpg>
Header add Link ‘<http://www.tomanthony.co.uk/httest/product-page.html>; rel=”canonical”‘
We could also use some variables magic (you didn’t know .htaccess could do variables!?) to apply this to all PDFs in a folder, linking back the HTML page with the same name (be careful with this if you are unsure):
RewriteRule ([^/]+).pdf$ – [E=FILENAME:$1]
<FilesMatch “.pdf$”>
Header add Link ‘<http://www.tomanthony.co.uk/httest/%{FILENAME}e.html>; rel=”canonical”‘

3.      Robots directives
Search engines will index a page unless you stop them to access the page. Even if you have blocked your page with robot.txt, Google might still index it if it has lot of links pointing to it. Here also, you can save yourself with the .htaccess snippet. You can apply directives to all files in a directory by creating an .htaccess file in that directory and adding this command:
Header set X-Robots-Tag “noindex, noarchive, nosnippet”

4.      Various types of redirect
The common SEO redirect is ensuring that a canonical domain is used, normally www vs. non-www. There are also a couple of other redirects you might find useful. I have kept them simple here, but often times you will want to combine these to ensure you avoid chaining redirects:
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]

5.      Custom 404 error page
A custom 404 error page will entertain your visitor even after the broken URL. This page will even redirect the user to get back on track instead of a white error page with black techno babble. You can also end up getting lots of links and traffic if you but your time and effort into a cool 404 page, like Distilled’s:
This is very easy to setup with .htaccess:
ErrorDocument 404 /cool404.html
# Can also do the same for other errors…
ErrorDocument 500 /cool500.html

6.      Send the Vary header to help crawl mobile content
As mobile is another important source of Goggling and you are having a single URL to serve on main as well as on mobile. Then using responsive design you are altering the HTML, then you should be using the ‘Vary’ header to let Google know that the HTML changes for mobile users. It will help your Google to crawl and index your pages appropriately.


Again, this is pretty simple to achieve with your .htaccess file, independent of your CMS or however you are implementing the HTML variations:  Header append Vary User-Agent

7.      Improve caching for better site speed
For better speed in main and specifically in mobiles as much visitors are coming from mobile networks. Before implementing this code to your snippet be sure about the caching system or caching length in your system to make sure about the speed. However, if you want a quick and easy solution to set the number of seconds, you can use the below. Here I set static files to cache for 24 hours:
<FilesMatch “.(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$”>
Header set Cache-Control “max-age=28800″

8.      An Apple-style ‘Back Soon’ maintenance page
Like an Apple famous shows “Back Soon” when they are planning for some new products and not in the market for stint. The message when you are doing the significant changes to redirect users to such pages is the best and useful at that time.
With this bit of .htaccess goodness, you can redirect people based on their IP address, so you can redirect everyone but your IP address and (this is a special ‘home’ IP address):
RewriteCond %{REMOTE_ADDR}  !your_ip_address
RewriteCond %{REMOTE_ADDR}  !
RewriteRule !offline.php$ http://www.example.com/back_soon.html [L,R=307]

9.      Smarten up your URLs even when your CMS says “No!”
One of the biggest complaints I hear amongst SEOs is about how much this or that CMS “sucks.” It can be intensely frustrating for an SEO when they are hampered by the restraints of a certain CMS, and one of those constraints is often that you are stuck with appaling URLs.
You can overcome this, turning product.php?id=3123 into /ray-guns/ in no time at all:
# Rewrite a specific product…
RewriteRule ray-guns/ product.php?id=3123

# … or groups of them
RewriteRule product/([0-9]+)/ product.php?id=$1
This won’t prevent people from visiting the crappy versions of the URLs, but combined with other redirects (based on IP) or with judicious use of rel=”canonical,” you improve the situation tremendously. Don’t forget to update your internal links to the new ones.

10.  Recruit via your HTTP headers
A HTTP header which can be very useful to send your message to the viewer with just a simple statement like “Email us for solution” or Get connected.  A custom header which is appropriate for your site and dose not even takes much space from you.


About us and this blog

Looking for a way to grow and enhance your marketing campaign online? Then your answer lies in expert SEO services from a trusted and reputed company that excels in the field of Search Engine Optimization

Get Free Quote

More from our blog

See all posts