sam
sam

Reputation: 27

mod_rewrite conflict

I am trying to give each MOVIE there own url name, for example, www.helloworld.com/BATMAN. I have been using mod_rewrite to create such url. I have been using following htaccess code to achieve such result.

 RewriteEngine ON
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-l

 RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

The mod_rewrite works perfectly. When A user enters randam movie name (name that does not exist in database), for example, www.helloworld.com/abcd, I have created a url redirect which is www.helloworld.com/oopsmovienotfound.php. This works fine too. But the problem I am having is this: When user click www.helloworld.com/login.php .The url keeps looping between login.php and oopsmovienotfound.php OR the url is forwarded to www.helloworld.com/oopsmovienotfound.php beacause database doesnot have "login.php" as a movie name. I thought this command

 RewriteCond %{REQUEST_FILENAME} !-f 

should have take care of this but its not. Please help. Following is the rewrite log.

[18/Jan/2012:15:51:41 -0500] "GET /movie/batman HTTP/1.1" 200 102262
[18/Jan/2012:15:51:42 -0500] "GET /movie/text.css HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/selectMovie.js HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/selectMoiveName.js HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/finalselection.js HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/categories.png HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/background.png HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/tinylogo2.png HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/header.png HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/selectState.js HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/content_tail.jpg HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/movieimages/movieimage.jpg HTTP/1.1" 304 -
[18/Jan/2012:15:51:42 -0500] "GET /movie/tab.png HTTP/1.1" 304 -
[18/Jan/2012:15:52:24 -0500] "GET /movie/login.php HTTP/1.1" 200 3330
[18/Jan/2012:15:52:24 -0500] "GET /movie/login.php HTTP/1.1" 200 3330
[18/Jan/2012:15:52:24 -0500] "GET /movie/img/bg.jpg HTTP/1.1" 302 102262
[18/Jan/2012:15:52:29 -0500] "GET /movie/img/oops.php HTTP/1.1" 302 102262
[18/Jan/2012:15:52:29 -0500] "GET /movie/img/oops.php HTTP/1.1" 302 102262
[18/Jan/2012:15:52:33 -0500] "GET /movie/img/oops.php HTTP/1.1" 302 102262

When I enter the URL localhost/movie/index.php the URL is forwarded to localhost/movie/batman because batman was the movie I was browsing before I closed my previous session. But when I enter localhost/movie/login.php the URL keeps looping. Funny thing there is no such folder called "img" in the server. why does it keep looping through localhost/movie/img/oops.php when there is no such path?

Upvotes: 0

Views: 401

Answers (1)

Olivier Pons
Olivier Pons

Reputation: 15778

Try to do the opposite first e.g. if it's a file, stop immediately, like this:

RewriteEngine ON
RewriteCond %(REQUEST_FILENAME) -d [OR]
RewriteCond %(REQUEST_FILENAME) -f [OR]
RewriteCond %(REQUEST_FILENAME) -l
RewriteRule (.*) - [QSA,L]

# all the other cases, apply rewrite:
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

And if that's not enough:

Two hints:

If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Upvotes: 2

Related Questions