views:

25

answers:

3

I want to create a rule at the end of an .htaccess file that catches everything that failed to match up until then.

How can I do that?

P.S. I've already tried everything :) Actually, I haven't, but it sure seems like it!

Update: Some people are replying with RewriteRule ^.*$ or an equivalent. This does not work! It will match everything including the other 'good' URLs.

A: 

RewriteRule .* page.php [L] ?

webdestroya
But then this matches everything including the good stuff.
George Edison
If you put this at the end, then all the good stuff should have been matched prior.
webdestroya
@webdestroya: Allow me to quote from someone on IRC: `.htaccess files are re-evaluated over and over and over and over and over until the rules stop matching.` And my experimentation agrees with this.
George Edison
A: 

It sounds like you want to look at the RewriteRule directive. Quie possibly something similar to the following:

RewriteRule ^/(.+) {target}

This will match at the root URL (/) and redirect it to the {target} url.

Sean Madden
But this will cause **all** URLs to go the specified target.
George Edison
A: 

There are actually some good answers here already, but you have responded with...

But then this matches everything including the good stuff.

This is because you aren't telling mod_rewrite to stop processing on a match. To do this, use the "L" tag after each rule, which tells mod_rewrite that "If this rule is matched, stop processing any further rules".

RewriteRule ^RSS/([^/\.]+)/?$ rss.php?Page=$1 [L]

You need to put this after EACH rule. Then, when you put the catch all at the end, it will only be hit if no other rule has been matched.

NOTE: if you are ALSO serving up resources that are not rewritten, like CSS, images, javascript files - you are honestly better off not catching all as you wouldn't want to rewrite their locations.

Sohnee
I tried that. [L] doesn't work in .htaccess files apparently - only in `vhosts.conf` and `apache2.conf`.
George Edison
Also, the CSS, images, etc. have a separate rule - I just omitted it from the question.
George Edison
[L] definitely works in .htaccess files - I'm using it!
Sohnee