imkingdavid
imkingdavid

Reputation: 1389

mod_rewrite URL masking is not cooperating

I have the following in my .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^task/(.*)/?$ index.php?mode=task&id=$1 [L]
RewriteRule ^(.*)/?$ index.php?mode=$1 [L]

If the last line is included, no matter what is put in the URL it sets $_GET['mode'] to index.php. Without the last line included, or if I set it to go to index.php?mode=home, for instance, it works fine, but there isn't a catchall.

I don't see what the problem is, but it's probably something simple. If someone else could take a moment to steer me right, that'd b great. Thanks!

Upvotes: 0

Views: 128

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

When you apply that to ,say, /task/123, this is what happens, (assuming that URI doesn't exist):

  1. passes !-f, /task/123 isn't a file that exists
  2. passes !-d, /task/123 isn't a directory that exists
  3. MATCH against ^task/(.*)/?$ so the URI gets rewritten to index.php?mode=task&id=123
  4. With [L], nothing else happens and the request gets INTERNALLY REDIRECTED to index.php?mode=task&id=123
  5. Internal redirect gets all rules re-applied <--- this is what's screwing you up
  6. no match against ^task/(.*)/?$, do nothing
  7. MATCH against ^(.*)/?$, so the URI gets rewritten to index.php?mode=index.php
  8. initial URL equal rewritten URL: index.php, stop rewriting

What you need to do is add a condition to the 2nd rule:

RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)/?$ index.php?mode=$1 [L]

Upvotes: 1

Related Questions