Forum Moderators: phranque

Message Too Old, No Replies

Mod_Rewrite Another PITA

mod_rewrite 404 error_log RewriteRule RewriteCond

         

meikeric

1:03 am on Aug 1, 2005 (gmt 0)

10+ Year Member



Here is my mod_rewrite:
-----------------------------------------------------
RewriteEngine On

RewriteBase /

#
# Rules
#
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]

##RewriteRule ^webcams.html /webcams.php [L]
RewriteRule ^webcams/?([^/]+)?/?([^/]+)?/?([^/]+)?\.html$ webcams.php?page=$3&id=$2&motion=$3 [L]

RewriteRule ^(.*) /index.php

---------------------------------------------------

I can access [mydomain.com...] but I CANNOT access [mydomain.com...] [mydomain.com...]
[mydomain.com...]

The ##commented out rule was to test because it wasn't working at all earlier.
When I attempt to access /webcams/cam.html or the others I get a 404 error, as well my ErrorLog states that the page is not available.
Does anyone know why it is not rewriting the url?
I've tested this with regexbuddy and it validates the RegEx expression.
The last rule is a default. I run Mambo CMS with SEF URL's enabled. All Mambo URL's work, ie. [mydomain.com...]
Thank you in advance for all replies.

jdMorgan

1:50 pm on Aug 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



meikeric.

Welcome to WebmasterWorld!

I'd guees that the reason is the left-to-right 'precedence' of your optional subpatterns, with a particular problem in the last subpattern, which could match the entire 'URL-tail' of "somepage.html" because it contains "^/" instead of "^.".

I would suggest a series of three simpler, more-specific rules, at least as an interim approach:


RewriteRule ^webcams/([^/]+)/([^/]+)/([^.]+)\.html$ /webcams.php?page=$1&id=$2&motion=$3 [L]
RewriteRule ^webcams/([^/]+)/([^.]+)\.html$ /webcams.php?page=$1&id=$2 [L]
RewriteRule ^webcams/([^.]+)\.html$ /webcams.php?page=$1 [L]

Because the back-reference "$3" appears twice in your example, I am not sure what your desired URL-part - to - query-string-parameter mapping is, so the above should be taken as an example only of how to prevent the failure you are currently seeing; The back-reference numbering and parameter names will likely need to be tweaked.

Jim

meikeric

3:53 pm on Aug 1, 2005 (gmt 0)

10+ Year Member



Thank Jim, as taken from Apache's page on mod_rewrite...
`` Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo. ''
-- Brian Moore

This is the config I ended up with, and it works GREAT!
------------------------------------------------------
#Turn me on baby ;-)
RewriteEngine On

#Not needed, base defaults to /
##RewriteBase /

#
# Rules
#
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]

#If webcams.html -> webcams.php
RewriteRule ^webcams.html /webcams.php [L]
#If webcam/images/1.jpg -> /webcams.php?page=thumbnail&id=1
RewriteRule ^webcam/images/([^.]+)\.jpg$ /webcams.php?page=thumbnail&id=$1 [L]

#If webcam.html -> webcams.php
#If webcam/cam.html -> webcams.php?page=cam
#If webcam/cam/1.html -> webcams.php?page=cam&id=1
#If webcam/cam/1/1.html -> webcams.php?page=cam&id=1&motion=1
RewriteRule ^webcam/?([^/]+)?/?([^/]+)?/?([^/]+)?\.html$ /webcams.php?page=$1&id=$2&motion=$3 [L]

##Pass anything else to main script.
RewriteRule ^(.*) /index.php [L]
--------------------------------------------------
I added in lots of comments so hopfully in the future, no one else will have to take as much time as I did.

Jim, I did indeed make a mistake in on the backtract, but that wouldn't have generated a 404, my script has built in error checking for stuff like that.

I knew my Regex was correct because I used Regex buddy, which I recommend.

What was happening is that I have a directory called webcams and webcams/images. This is used to store real images. So mod_rewrite saw the directory (I assume) and wouldn't let it enter the ruleset when a page like ^/webcams/cam/1.html or ^/webcams/images/1.jpg

Anyways, I hope this helps someone in the future. I know my main regex is complicated, but it works, and I understand it.