views:

694

answers:

2

I have a server, it's httpd.conf already has some "RedirectMatch permanent" directives in it.

I'm not that familiar with mod_alias, I've only ever used mod_rewrite.

What's the basic difference? I don't see a "L" flag in mod_alias to stop processing rules.

Which one should I use for best practices of redirecting from one subdomain to another?

Can I use both at the same time and will it be obvious which takes precidence?

+2  A: 

mod_alias is basically a simpler version of mod_rewrite. It can't do some things that mod_rewrite can, such as manipulate the query string. If you're able to choose either of them, I don't see any reason that you'd want to use mod_alias.

Is there a specific reason you need to try to use both together?

This seems to be a good article about the two: Apache mod_rewrite & mod_alias tricks you should know. It notes at one point that mod_rewrite rules get executed before mod_alias ones.

Chad Birch
The only reason I want to use both together, is that there are about 30 existing rules using mod_alias. I've written my own mod_rewrite rules that I'd like to add, and I'd rather not have to recreate the existing rules in mod_rewrite.
jeph perro
A: 

One of the more obvious differences between these two ways of "rewriting" URLs is the fact that, well, mod-alias does NOT rewrite URLs at all. That is not what it does. It maps URLs to filesystem paths, which is a slightly different thing but is nevertheless a very important fact to consider here. After your realize the difference, it is easy to understand exactly why mod-rewrite processing takes precedence before mod-alias - filesystem mapping is just about the end of the road in the long chain of Apache finding what to do with an URL, and since the latter maps an URL to a file, it has to follow, not precede, the rewriting of an URL by the former.

Since mod-alias maps URLs to real file locations, you obviously cannot use it to even rewrite query strings - since it does not "rewrite" URLs but maps URLs to files, the following will not work as some novices may expect it to:

AliasMatch ^/product/([[:alnum:]_]+)$ /scripts/products/get_product.php?id=$1

Apache will interpret the directive above as: if the URL matches the regular expression (1), serve file by the path (2) (numbers denote directive parameters). So, when you request /product/shinytoy, it will, according to the directive above, want to fetch the file at filesystem location /scripts/products/get-product.php?id=shinytoy VERBATIM, which is a very unusual filename indeed and most probably not the kind of procedure you expected. Yes, ...?id=shinytoy IS part of the filename. Unless you want to have each of your product URLs to result in looking up and handling files with these kind of names, you are definitely looking for the kind of functionality mod-rewrite provides.

Evidently though, mod-alias can be somewhat faster than mod-rewrite, all other things being equal of course and when it can keep up. So, if mod-alias can do it for you, and you have a very high volume website, prefer setting up aliases like above (well, actually not, the example is hardly exemplary) rather than rewriting URLs.

Last but not least, the inability of mod-alias to rewrite URLs on its own does not mean you cannot get away using it to achieve exactly that. How? Well, consider with the above example that your get-product.php script still has access to the URL string (via -SERVER['REQUEST-URI'] variable, for one), which is still what the URL string originally was - /product/shinytoy. And so, it can extract the product identifier from that string and in effect still be able to branch correct procedure for fetching the right product, or whatever you want.

P.S. All underscores in this writing were replaced with dashes, because the formatting would not work reliably otherwise.

amn
So mod_rewrite rules get parsed wayyyy before mod_alias even gets a look in? If so, how does apache know where to go and find my .htaccess file (where my mod_rewrite rules are) if it's in a directory only web-accessible because of an Alias in the httpd.conf file?
Matt