RewriteRule .? only for one or more chars?

1.9k views Asked by At

On the net I found several ways of defining a RewriteRule like:

RewriteRule .? 404.php [L]
RewriteRule . 404.php [L]
RewriteRule ^ 404.php [L]

But why would these work when entering a garbage url? Because the first (.?) means that there should be zero or one char which would lead to 404.php (not a whole bunch of chars or a word). Likewise, . and ^ look only for a char and a start of a line (not a line with a few chars or words, right?), respectively. That's what I think based on the RegEx help. Am I wrong? Or ...?

2

There are 2 answers

0
Gumbo On

They work because they probably match that “garbage URL”. Because .? matches zero or one characters, . matches a single character, and ^ matches just the begin of a string (no matter what follows).

The question is rather, how you differentiate between “garbage” and “non-garbage” URLs.

1
Carlos Campderrós On

I haven't tested, but I think this will redirect absolutely any request to 404.php.

The regex .? will search for 0 or 1 character, any character, at any position of the url. So basically, any url will match the first regex and will redirect.

The [L] at the end of the line makes mod-rewrite to not process any more rules when it founds a match.