Alwayz Change
Alwayz Change

Reputation: 225

Rewrite module, cannot match the dot character?

I have a site using Apache rewrite module. The problem is that I'm using the RewriteRule like this:

RewriteRule ^([^/]+)/?$ /index.php?p1=$1 [L]

I need to match all the characters except "/", but it doesn't work. It counter an error "The requested URL was not found on this server."

it work with this rule:

RewriteRule ^([^/\.]+)/?$ /index.php?p1=$1 [L]

but this rule will not match the "dot", so whenever the url have the "dot" it will counter the same as above.

Please help

Upvotes: 1

Views: 347

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

RewriteRule ^([^/]+)/?$ /index.php?p1=$1 [L]

The reason that isn't working is because there is an internal redirect loop, let's say you are getting a request /zoo:

  1. zoo (no leading slash here) matches ^([^/]+)/?$ and the url gets rewritten to /index.php?p1=zoo
  2. zoo and /index.php are different so it gets internally redirected, and the rules are applied again, leading slash stripped
  3. index.php matches ^([^/]+)/?$, gets rewritten to /index.php?p1=index.php
  4. index.php and /index.php are different, so internal redirect again
  5. repeat 3 and 4

One way you can stop the loop is to change the rule to:

RewriteRule ^([^/]+)/?$ index.php?p1=$1 [L]

So that index.php would match index.php (no leading slash), but a better way would be to add some rewrite conditions:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

In front of the rewrite rule.

Upvotes: 1

Related Questions