www.pokeroconnor.com

htaccess Tips and Tricks

April29

Good information on htaccess (mod_rewrite and mod_alias) can be tough to come by – one web “guru” I know has a theory that mod_rewrite is one of the black arts of web design. So in order to try and shine a light on the darkness of htaccess, here’s some of the useful and very practical tidbits I’ve come across. 

To start with, here’s some of the better resources, with a brief description of what each link has to offer.

  • Stupid htaccess tricks is probably the most comprehensive guide to htaccess, and deserves to be first in the list. The examples aren’t very detailed, but if you work with mod_rewrite you will likely need to check something here
  • fun with htaccess is a more descriptive piece, nowhere near as comprehensive as Stupid Tricks, with just a few samples, but nicely written

Actually, the above two are the main ones worth reading, even if you aren’t looking for  anything specific. To be more specific, let’s look at some sample use cases.

Let’s jump right in – to use htaccess effectively you will be using regular expressions, for example say you want to match a certain string, and redirect that plus everything that follows. E.g. you have many many urls such as my.domain.com/book123 my.domain.com/book456 my.domain.com/book789 and you want to redirect them all to the uri /new-books, but keep the numbers, e.g. my.domain.com/book123 is to go to my.domain.com/new-books123 etc. So you would use a regexp, which is defined on the left hand side of the rule, and “used” on the right hand side. $1 represents the first match of a pattern (which has previously matched in that particular rule), $2 the second and so forth. So, that gives us:

RewriteRule ^book(.*)$ /new-books$1 [L,NC,R=301]

The ^ symbol signifies beginning of the match, $ the end. .* matches everything. Using parentheses around the .* identifies this as a matched pattern, for use on the right hand side with $1. [L,NC,R=301] is a sample of many possibilities, L meaning this is a terminating rule for that match, NC ignore case, 301 is a permanent redirect. Stupid htaccess tricks has a full list of all these options. The regexp’s you can use are endless of course!

Sometimes you need to build compound, conditional rules, such as if a certain domain, then redirect a certain uri. E.g. say you have 3 domains, www.domain.com www.domain.net and www.domain.org, and you want requests to domain.com/net/org to redirect to www.domain.com/net/org.

RewriteCond %{HTTP_HOST} ^domain\.(com|net|org) [NC]

RewriteRule ^(.*)$ http://www.domain.%1/$1 [R=301,L]

In this case the first pattern is com|net|org, which is represented by %1 in the rule. $1 again matches (.*) which means for example that domain.org/blah gets redirected to www.domain.org/blah. You can chain as many conditions as you need. 

htaccess ignores query strings, so if you needed to match www.domain.com/index?key=blahblah and redirect it to www.domain.com/index you would be out of luck! Unless of course you do this:

RewriteCond %{REQUEST_URI}  ^/index$

RewriteCond %{QUERY_STRING} ^key=blahblah$

RewriteRule ^index/?(.*)$ http://www. domain.com/index/? [R=301,L,NC]

Note the ‘?’ at the end of the rule, in http://www. domain.com/index/This is vital and signifies that you want to drop the query string

Say you want to block some bozo’s IP from accessing your site. Simply add this:

order allow,deny
deny from 123.45.6.7
deny from 012.34.5.
allow from all

Just make sure you use that order above to deny…

That’s it for now, there is so much to add, and so little time to add it!

Email will not be published

Website example

Your Comment: