Top tip: Only use one .htaccess

Disclaimer: I’m trying to write this in under 7 minutes

It’s been a fun day, and we’ve learnt something from this:

Only use one .htaccess file for mod_rewrites.

Example:

  • Say I have my site, path-wise located at /
  • My site also has a shop, located at /shop/

Now, it’s really tempting to have a /.htaccess file containing all my rewrites for my site, and a separate /shop/.htaccess file which holds just the shop-specific rewrites. It’s modular, it’s scalable, it’s clear.

It’s a mistake.

The problem is that your /shop/.htaccess not only overrides /.htaccess for all the /shop/.* URLs, but it actually *disables* all the rewrites for /.htaccess. This is a problem if for example you one day decide to do any mass site-wide redirects, such as domain or protocol canonicalisation. Cleaning up those old URLs can be a problem too.

However, I like modulation so I’m going to do some thinking about this and see what I can come up with as a solution.

Enjoy your weekend!

-Rob

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • Live
  • StumbleUpon
  • Facebook
  • Google
  • Reddit
  • Technorati

8 Comments | Leave a comment »

  1. 1. BrianL | June 8th 2007 @ 9:37 pm

    I would be interested in seeing how well that idea scales. What if, as an example, you have a site with 50 different directories, each with its own rewrite rules, as well as 5 301 redirects needed per directory. Wouldn’t a large .htaccess file result in a pretty high demand on the server?

  2. 2. XRider | June 8th 2007 @ 9:37 pm

    Ok thanks for letting us know - I was planning on doing something exactly like that.

  3. 3. Lea de Groot | June 9th 2007 @ 6:05 am

    Yes, its one of the ‘problems’ with Apache.
    I wonder what would be better done in the httpd.conf (for the rare host that offers access). Probably not worth investigating, given how hard it is to find hosting that offers it.
    Minimising the number of .htaccess files minimises the number of sources of problems, but sometimes you can’t get it down to 1 - a 3rd-party application dropped in a subdirectory will probably need its own .htaccess :(

  4. 4. Rob Haswell | June 9th 2007 @ 10:19 am

    Perhaps the only solution is vigilance.

  5. 5. Sean | June 10th 2007 @ 2:53 pm

    .htaccess is processed in order as you said, and some things don’t play well with that. If you’re going to rewrite, you need to do it at the top level.

    However if you need to provide per directory configuration, such as access control, indexes, etc, you need a per directory .htaccess, or move to the main httpd.conf. Location and Directory containers are not valid within .htaccess, so you can’t do those at the top level, they need to be in the directory they affect.

    If you need a set of common rewrite rules within each .htaccess, I’d suggest templating the .htaccess and using Makefiles to ensure they’re up to date.

    Sean

  6. 6. Kevin | June 11th 2007 @ 8:50 am

    If you have access to the main config file like suggested above it’s fairly sane and easy. Make a rewrite directory for that host and use a wildcard include to suck in all the files you place there - which is basically your .htaccess files with Directory tags added (although obviously no promises - different .htaccess files require different changes to make them run in the main config).

    Apache config includes are great for making anything you like modular.

  7. 7. Dara | December 8th 2007 @ 7:31 pm

    Maybe the apache team can come up with a working solution ?

  8. 8. Adaptiv Media | April 10th 2008 @ 3:09 pm

    What came about of this? Thanks for confirming the overwriting of the root .htaccess by the way. I’d always wondered why my Regular Expressions weren’t always working properly for some directories. Turns out it probably wasn’t my Regex’s that were the problem after all. Thanks.

Leave a Reply

required

required, hidden

smx

Start with £50 credit in your new Yahoo! Search Marketing account for a limited period only.

+ Advertise Here