1

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 ...?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

2 Answers2

0

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.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

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.

Carlos Campderrós
  • 22,354
  • 11
  • 51
  • 57
  • Also, `.` or `^` will match any url, because regex look at any position of the url in the first one (to match urls of just one character you should write `^.$`, that is, beginning of line, one character and then the end of line). If you omit the `$` it reads as any line that has a character after the beginning of line, but not limiting for any (possible) following chars – Carlos Campderrós Feb 22 '11 at 12:10