Bryan Hadaway's Blog
Your Account:   subscribe

bhadaway@gmail.com | Social: Facebook Twitter LinkedIn Google+

Dynamic SEO-Friendly .htaccess File





How to Add to Your Website

Download this zip file, extract and upload the .htaccess file to the root folder of your website via FTP. This folder is usually called public_html.

Code Refresh | July 20, 2011

The following code section will from this point on represent my recommendation for a starter .htaccess script for any website:

Options -MultiViews +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteRule ^/?(.*/?)index\.(htm|html|php) /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

ErrorDocument 404 /

<Files error_log>
order allow,deny
deny from all
</Files>

You might also consider adding the following list to your .htaccess file to block bad robots.

Each Section Explained

Options -MultiViews +FollowSymlinks
RewriteEngine on

Turns the rewrite engine on.

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Enforces non-www on your URLs.

RewriteRule ^/?(.*/?)index\.(htm|html|php) /$1 [R=301,L]
Removes index.php, index.html and index.htm from http://example.com/index.php so it becomes a cleaner more professional http://example.com/.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Allows you to use more attractive URLs. Instead of http://example.com/about.php you can use http://example.com/about.

ErrorDocument 404 /
Direct 404 errors (page not found) to your homepage, sitemap or wherever you like.

<Files error_log>
order allow,deny
deny from all
</Files>

Keep prying eyes from being able to see your server’s (website’s) error log.

Let me know if you see any mistakes, where there could be improvements and if there should be any key additions.

Old

Options -MultiViews +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteRule ^/?(.*/?)index\.(htm|html|php) /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_URI} !^/search-results
RewriteCond %{QUERY_STRING} !=””
RewriteRule ^(.*)$ /$1? [R=301,L]

ErrorDocument 404 /sitemap

I use this file for the FREEWebPlate website template I built | View Demo

In order from top to bottom, I’ve marked parts in bold that you may want to alter:

- index | you may change this to: default if that’s what you’re using

- htm|html|php | you may add more extensions like so: htm|html|php|asp

- php (both places) | you may change to any other file type extension; htm, html etc.

- /search-results | you’ll need to specify what folders/files you want to allow query strings (?) on, add a new line for each

- /sitemap | you’ll need to specify what page/URL you want 404 pages to go to

For more advanced users, you can of course alter it however you like.

What this Script Does

  • Enforces www in URLs
  • Removes index.htm, index.html and index.php from URLs
  • Allows you to link to cleaner versions of URLs without using the extension (use website.com/contact instead of website.com/contact.php)
  • Strips query strings from URLs except for the folders and files you allow
  • Forwards/redirects 404 error pages to the page of your choice

Enjoy.

Thanks for reading, Bryan

Other Reads