10 .htaccess File Snippets You Should Have Handy – Moz
Skip to content
Moz logo
Menu open
Menu close
Search
Products
Moz Pro
Moz Pro Home
Moz Local
Moz Local Home
STAT
Mozscape API
Free SEO Tools
Competitive Research
Link Explorer
Keyword Explorer
Domain Analysis
MozBar
More Free SEO Tools
Learn SEO
Beginner’s Guide to SEO
SEO Learning Center
Moz Academy
SEO Q&A
Webinars, Whitepapers, & Guides
Blog
Why Moz
Agency Solutions
Enterprise Solutions
Small Business Solutions
Case Studies
The Moz Story
New Releases
Log in
Log out
Products
Moz Pro
Your All-In-One Suite of SEO Tools
The essential SEO toolset: keyword research, link building, site audits, page optimization, rank tracking, reporting, and more.
Learn more
Try Moz Pro free
Moz Local
Complete Local SEO Management
Raise your local SEO visibility with easy directory distribution, review management, listing updates, and more.
Learn more
Check my presence
STAT
Enterprise Rank Tracking
SERP tracking and analytics for SEO experts, STAT helps you stay competitive and agile with fresh insights.
Learn more
Book a demo
Mozscape API
The Power of Moz Data via API
Power your SEO with the proven, most accurate link metrics in the industry, powered by our index of trillions of links.
Learn more
Get connected
Compare SEO Products
Free SEO Tools
Competitive Research
Competitive Intelligence to Fuel Your SEO Strategy
Gain intel on your top SERP competitors, keyword gaps, and content opportunities.
Find competitors
Link Explorer
Powerful Backlink Data for SEO
Explore our index of over 40 trillion links to find backlinks, anchor text, Domain Authority, spam score, and more.
Get link data
Keyword Explorer
The One Keyword Research Tool for SEO Success
Discover the best traffic-driving keywords for your site from our index of over 500 million real keywords.
Search keywords
Domain Analysis
Free Domain SEO Analysis Tool
Get top competitive SEO metrics like Domain Authority, top pages, ranking keywords, and more.
Analyze domain
MozBar
Free, Instant SEO Metrics As You Surf
Using Google Chrome, see top SEO metrics instantly for any website or search result as you browse the web.
Try MozBar
More Free SEO Tools
Learn SEO
Beginner’s Guide to SEO
The #1 most popular introduction to SEO, trusted by millions.
Read the Beginner’s Guide
How-To Guides
Step-by-step guides to search success from the authority on SEO.
See All SEO Guides
SEO Learning Center
Broaden your knowledge with SEO resources for all skill levels.
Visit the Learning Center
Moz Academy
Upskill and get certified with on-demand courses & certifications.
Explore the Catalog
On-Demand Webinars
Learn modern SEO best practices from industry experts.
View All Webinars
SEO Q&A
Insights & discussions from an SEO community of 500,000+.
Find SEO Answers
August 7-9, 2023
Lock in Super Early Bird savings for MozCon
Snag tickets
Blog
Why Moz
Small Business Solutions
Uncover insights to make smarter marketing decisions in less time.
Grow Your Business
The Moz Story
Moz was the first & remains the most trusted SEO company.
Read Our Story
Agency Solutions
Earn & keep valuable clients with unparalleled data & insights.
Drive Client Success
Case Studies
Explore how Moz drives ROI with a proven track record of success.
See What’s Possible
Enterprise Solutions
Gain a competitive edge in the ever-changing world of search.
Scale Your SEO
New Releases
Get the scoop on the latest and greatest from Moz.
See What’s New
New Feature: Moz Pro
Surface actionable competitive intel
Learn More
Log in
Moz Pro
Moz Local
Moz Local Dashboard
Mozscape API
Mozscape API Dashboard
Moz Academy
Avatar
Moz Home
Notifications
Account & Billing
Manage Users
Community Profile
My Q&A
My Videos
Log Out
By: Tom Anthony
December 11, 2012
10 .htaccess File Snippets You Should Have Handy
Technical SEO
The author’s views are entirely his or her own (excluding the unlikely event of hypnosis) and may not always reflect the views of Moz.
In the Moz Q&A, there are often questions that are directly asked about, or answered with, a reference to the all-powerful .htaccess file. I’ve put together a few useful .htaccess snippets which are often helpful. For those who aren’t aware, the .htaccess file is a type of config file for the Apache server, which allows you to manipulate and redirect URLs amongst other things.
Everyone will be familiar with tip number four, which is the classic 301 redirect that SEOs have come to know and love. However, the other tips in this list are less common, but are quite useful to know when you need them. After you’ve read this post, bookmark it, and hopefully it will save you some time in the future.
1) Make URLs SEO-friendly and future-proof
Back when I was more of a developer than an SEO, I built an e-commerce site selling vacations, with a product URL structure:
/vacations.php?country=italy
A nicer URL would probably be:
/vacations/italy/
The second version will allow me to move away from PHP later, it is probably better for SEO, and allows me to even put further sub-folders later if I want. However, it isn’t realistic to create a new folder for every product or category. Besides, it all lives in a database normally.
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’):
http://www.tomanthony.co.uk/httest/magic/foo/bar/donk
2) Apply rel=”canonical” to PDFs and images
The SEO community has adopted rel=”canonical” quickly, and it is usually kicked around in discussions about IA and canonicalization issues, where before we only had redirects and blocking to solve a problem. It is a handy little tag that goes in the head section of an HTML page.
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. This suggestion came from a client of mine recently, and is something a couple of us had kicked about once before in the Distilled office. My first reaction to the client was that this practice sounded a little bit ‘dodgy,’ but the more I think about it, the more it seems reasonable.
They had a product range that attracts people to link to their images, but that isn’t very helpful to them in terms of SEO (any traffic coming from image search is unlikely to convert), but rel=”canonical” those links to images to the product page, and suddenly they are helpful links, and the rel=”canonical” seems pretty reasonable.
Here is an example of applying HTTP rel=”canonical” to a PDF and a JPG file:
Header add Link ‘
Header add Link ‘
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]
Header add Link ‘
You can read more about it here:
http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394
3) Robots directives
You can’t instruct all search engines not to index a page, unless you allow them to access the page. If you block a page with robots.txt, then Google might still index it if it has a lot of links pointing to it. You need to put the noindex Meta Robots tag on every page you want to issue that instruction on. If you aren’t using a CMS or are using one that is limited in its ease, this could be a lot of work. .htaccess to the rescue!
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”
If you want to read a bit more about this, I suggest this excellent post from Yoast:
http://yoast.com/x-robots-tag-play/
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
None of your visitors should be seeing a white error page with black techno-babble when they end up on at a broken URL. You should always be serving a nice 404 page which also gives the visitor links to get back on track.
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
If you are serving a mobile site on the same URLs as your main site, but rather than 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. This helps them to crawl and index your pages more appropriately:
https://developers.google.com/webmasters/smartphone-sites/details
Again, this is pretty simple to achieve with your .htaccess file, independent of your CMS or however your are implementing the HTML variations:
Header append Vary User-Agent
7) Improve caching for better site speed
There is an increasing focus on site speed, both from SEOs (because Google cares) and also from developers who know that more and more visitors are coming to sites over mobile connections.
You should be careful with this tip to ensure there aren’t already caching systems in place, and that you choose appropriate caching length. 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:
Header set Cache-Control “max-age=28800”
8) An Apple-style ‘Back Soon’ maintenance page
Apple famously shows a ‘Back Soon’ note when they take their store down temporarily during product announcements, before it comes back with shiny new products to love or hate. When you are making significant changes to redirect users to such a page, a message such as this can be quite useful. However, it can also make it tough to check the changes you’ve made.
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 127.0.0.1 (this is a special ‘home’ IP address):
RewriteCond %{REMOTE_ADDR} !your_ip_address
RewriteCond %{REMOTE_ADDR} !127.0.0.1
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
Ever looked closely at SEOmoz’s HTTP headers? You might have missed the opportunity to get a job…
If you would like to add a custom header to your site, you can make up whatever headers and values you’d like:
Header set Hiring-Now “Looking for a job? Email us!”
It can be fun to leave messages for people poking around – I’ll leave it to your imaginations! 🙂
Download the rules
You can grab all of these rules in quick-form from a compilation I made.
Viewing headers
If you are unsure about how to look at HTTP response headers, here’s a great tool to get you started.
If you would rather do it in your browser, follow these steps:
Chrome on Windows: Ctrl-Shift-I and click ‘Network’ (then reload the page)
Chrome on Mac: Command-Option-I and click ‘Network’ (then reload the page)
Firefox: Install Live HTTP Headers
Share yours!
Anything I missed, mistakes I made, or better ways to do something? Any cool ones you have up your sleeves? I’d love people to add their tips to the comments so I can come back to this post next time I get stuck. I’ll try to update my download file with any cool ones the community comes up with.
Thanks for reading, and don’t forget to test anything you change! 🙂
About Tom Anthony —
Tom is VP Product at Distilled, spending his time looking at technology trends and leading the product team building the DistilledODN SEO split testing platform. Follow him on Twitter: @TomAnthonySEO.
With Moz Pro, you have the tools you need to get SEO right — all in one place.
Start your free trial!
Read Next
How to Use Chrome to View a Website as Googlebot
Read this post
Underused Tactics and Overlooked Metrics in E-Commerce
Read this post
How We Increased Revenue with Speed Optimization [Local SEO Case Study]
Read this post
Comments
Please keep your comments TAGFEE by following the community etiquette
Comments are closed. Got a burning question? Head to our Q&A section to start a new conversation.
Moz logo
Contact
Community
Free Trial
Terms & Privacy
Jobs
Help
News & Press
Copyright 2022 © Moz, Inc. All rights reserved.