mod_rewrite tricks

Bookmark and Share
| 3 Comments
Mod_rewrite is an incredibly powerful tool and a lot of us would be very lost without it!
 A couple of quick "tricks" using mod_rewrite - partially to help myself find them again in the future:

 Redirecting domain.tld to www.domain.tld
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.tld$ [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]


 This can be put in a .htaccess in the website's root.
It will match any HTTP request that is NOT for www.domain.tld and rewrite it to www.domain.tld

Changing File Extensions

If you have changed file extensions on a site, for example moving all files from being ".htm" to ".php" then this line of code will save you a lot of hassle.
Not only will it help with any inbound links, but also for any internal links that you need to edit, but haven't:

 RewriteRule ^(.*).htm$ $1.php [R=301]

the first file extension in the line is the one you are replacing, while the second is the replacement.
  • Currently 5/5
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 5 /5( 1 votes cast)

latest funny tshirts

3 Comments

AddHandler application/x-httpd-php .html .htm

in a .htaccess file would be another idea.

You could do that, but why bother? :)

Having said that .. I've had to actually do that on here since I moved over to MT so that I could keep using a particular 3rd party service!

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.34-en
Get a personal domain - get a .me!
TwitterCounter for @mneylon

About this Entry

This page contains a single entry by Michele Neylon published on March 4, 2007 10:31 AM.

Yet Another Wordpress Security Hole was the previous entry in this blog.

Blog Awards 2007 Winners is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Recent Activity

Today