If you have worked with CMS systems you may have encountered the .htacess file. I you are not already familiar with what it is and how it is used, this lesson will help you learn how to do some really powerful things with provided code shippets... but you may want to take a moment to read this first, and then move forward with this lesson.
.htacces Rewrites are enabled using the Apache module mod_rewrite. It is one of the most powerful Apache modules and features available. .htaccess rewrites through mod_rewrite offer the ability to rewrite requests internally and to redirect requests externally.
This lesson will offer you code snippets for some of the most common rewrites. However, you should also bookmark this Mod_Rewrite Variables Cheat Sheet for reference anytime you are working on rewrites.
- RewriteRule !\.(html|php)$ - [S=5]
- RewriteRule ^.*-(vf12|vf13|vf5|vf35|vf1|vf10|vf33|vf8).+$ - [S=1]
One of the most common URL problems with any CMS system is that by default the URL structure is not friendly to search engines or to users. In a CMS the page is actually pieced together from information that lives in the database. Therefore, the URL by default will contain a bunch of database queries, strung together that represent the data that is being displayed. However, as discussed in the course “Site Architecture and Navigation” having a static keyword rich URL string offer significant benefits. Automatically rewrite dynamic URL to static URLs with the following:
- RewriteEngine On
- RewriteBase /
- RewriteRule ^([^/\.]+)\/?$ index.php?p=$1
If you are going to apply an SSL certificate to your site (sitewide) the following redirect will make sure that all of your http pages are redirected to https:
- RewriteEngine On
- RewriteCond %{HTTPS} !=on
- RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
It is preferable if your URL structure does not have .html at the end. Use this rule to remove it:
- RewriteEngine on
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteCond %{REQUEST_FILENAME}\.html -f
- RewriteRule ^(.*)$ $1.html
This is an issue on many sites where the URL for a page can be appended with a query string such as example.com/page?anything=anything. This is bad for SEO. Fix it with the following rewrite:
- RewriteRule ^getflash/?$ http://www.adobe.com/shockwave/download/download.cgi?
- P1_Prod_Version=ShockwaveFlash [NC,L,R=307]
Occasionally, URL rewrites cause infinite loops. Stop it with the following:
- Options +FollowSymLinks
- RewriteEngine On
- RewriteBase /
- RewriteCond %{HTTP_HOST} !^www\.askapache\.com$ [NC]
- RewriteRule ^(.*)$ http://www.askapache.com/$1 [R=301,L]
This allows you to update your JS and CSS files in your visitors cache simply by naming them differently in the html. On the server they stay the same. This rewrites: site.com/js/anything-anynumber.js to site.com/js/anything.js and site.com/c/anything-anynumber.css to site.com/c/anything.css
- RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC]
- RewriteRule .* - [L]
- RewriteCond %{ENV:REDIRECT_STATUS} 200
- RewriteRule .* - [L]
If your site offers multiple languages, you can set it to automatically detect the client’s preferred language and automatically switch.
- RewriteRule ^(.+)\.pdf$ /cgi-bin/pdf-script.php?file=$1.pdf [L,NC,QSA]
Search engines do not see underscores “_” as word brakes. Therefore, any brakes between words in a URL should use hyphens “-“. For sites that have this problem, here is a way to automatically rewrite all underscores in URLs to hyphens.
- Options +FollowSymLinks
- RewriteEngine On
- RewriteBase /
- # If the hour is 16 (4 PM) Then deny all access
- RewriteCond %{TIME_HOUR} ^16$
- RewriteRule ^.*$ - [F,L]
This is a problem where other sites can use your images on their site. Essentially, this allows them to display content to their visitors, and uses your bandwidth to do it. You get no benefit. Use the following to stop this from happening.
- RewriteEngine On
- RewriteBase /
- RewriteCond %{REQUEST_METHOD} !^(GET|PUT)
- RewriteRule .* - [F]
For additional technical documentation on this visit: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
For ASP.NET websites, check out this link for a deeply technical discussion on rewriting URLs: http://weblogs.asp.net/scottgu/tip-trick-url-rewriting-with-asp-net
Additional URL rewriting resources:
Free Open Source URL Rewriter for .net / IIS / ASP.NET: http://urlrewriter.net/
Free Open Source Rewrite Module tuned for ASP.NET 2.0: http://www.urlrewriting.net/162/en/faq.html
Open Source Apache Mod_Rewrite: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Top Paid Tool: ISAPI_rewrite engine: http://www.isapirewrite.com/
Now you know how to do some serious .htaccess VooDoo!
Next In the next lesson we will take a look at how to identify and deal with