3

I've got a site that was hosted on a linux el cheapo hosting service that I'm migrating to my Mac OS X 10.5 Leopard Server server running Apache 2.2.8 & PHP 5.2.5 w/rewrite_module enabled and AllowOverride All, but I'm running into an issue with the following lines in the .htaccess file:

RewriteEngine On
#RewriteRule ^view/([^/\.]+)/?$ /view.php?item=$1 [L]
#RewriteRule ^order/([^/\.]+)/?$ /order.php?item=$1 [L]
RewriteRule ^category/([^/\.]+)/?$ /category.php?category=$1 [L]

As you can see, I've commented out the RewriteRule directives for /view/ and /order/, so I'm only dealing with /category/. When I attempt to load http://domain.tld/category/2/ it runs category.php (I've added debug code to confirm), but $_SERVER['REQUEST_URI'] comes through as /category/2/ and $_GET['category'] comes through as empty.

I'm usually fine with troubleshooting .htaccess files and mod_rewrite directives, but this one's got me stumped for some reason.

Update: I followed Josh's suggestion and here's the what's dumped to mod_rewrite.log when I try to access http://domain.tld/category/2/:

65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (2) init rewrite engine with requested uri /category/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (3) applying pattern '.*' to uri '/category/13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (1) pass through /category/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6aa98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] add path info postfix: /Library/WebServer/Documents/tld.domain.www/category.php -> /Library/WebServer/Documents/tld.domain.www/category.php/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6aa98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] strip per-dir prefix: /Library/WebServer/Documents/tld.domain.www/category.php/13 -> category.php/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6aa98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] applying pattern '^category/([^/\.]+)/?$' to uri 'category.php/13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6aa98/subreq] (1) [perdir /Library/WebServer/Documents/tld.domain.www/] pass through /Library/WebServer/Documents/tld.domain.www/category.php
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] add path info postfix: /Library/WebServer/Documents/tld.domain.www/category.php -> /Library/WebServer/Documents/tld.domain.www/category.php/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] strip per-dir prefix: /Library/WebServer/Documents/tld.domain.www/category.php/13 -> category.php/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] applying pattern '^category/([^/\.]+)/?$' to uri 'category.php/13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (1) [perdir /Library/WebServer/Documents/tld.domain.www/] pass through /Library/WebServer/Documents/tld.domain.www/category.php
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (2) init rewrite engine with requested uri /13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (3) applying pattern '.*' to uri '/13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (1) pass through /13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] strip per-dir prefix: /Library/WebServer/Documents/tld.domain.www/13 -> 13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] applying pattern '^category/([^/\.]+)/?$' to uri '13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (1) [perdir /Library/WebServer/Documents/tld.domain.www/] pass through /Library/WebServer/Documents/tld.domain.www/13
morgant
  • 1,470
  • 6
  • 23
  • 33

3 Answers3

6

just add this to your .htaccess file

Options -MultiViews

i've had the same problem for hours

i don't know why, but it's turned on by default on OSX

it produces your error because when you try loading category/ it finds the category.php and redirects everything after the slash to that file

nemdub
  • 76
  • 1
  • 3
1

Do you have access to httpd.conf? If so please add to httpd.conf:

RewriteLog "/tmp/mod_rewrite.log" 
RewriteLogLevel 3

And post what that log says when you load /category/2. Remove this after you get the information you need from it -- it's very wasteful of server resources.

EDIT:

Thanks for the log. Something is rewriting the URL to: /category.php/13. Any other RewriteRules? Are you using FastCGI? What if you rename category.php to something else, like Category.php or display_category.php?

EDIT2:

Since something seems to be rewriting /category/(.*) to /category/$1, and we don't know what it is, why don't you change:

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

to:

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

and see if that works?

Josh
  • 9,190
  • 28
  • 80
  • 128
  • Done. See updated question. – morgant Oct 22 '09 at 21:43
  • No other rules. In fact, I fully deleted all but `RewriteEngine On` and `RewriteRule ^category/([^/\.]+)/?$ /category.php?category=$1 [L]` from the `.htaccess` file. Renaming `category.php` to `display_category.php` and changing the substitution to `/display_category.php?category=$1` worked, but I'd really like it to work with the file still named `category.php`. – morgant Oct 22 '09 at 21:54
  • 1
    Something is rewriting requests for `/category/(.*)` to `/category.php/$1`... Do you use FastCGI PHP? – Josh Oct 22 '09 at 22:05
  • Changing the RewriteRule to match against category.php also worked. Interestingly, `^view/` and `^order/` have the same issue... I'm not using FastCGI PHP, AFAIK, it's all a base install of Leopard Server. The only other mod_rewrite rules I know are applied are `RewriteCond %{REQUEST_METHOD} ^TRACE` and then `RewriteRule .* - [F]` and that's true for all sites. Maybe one of the other Apache modules (or the PHP5 module) is doing this for convenience? – morgant Oct 22 '09 at 22:29
  • Also, there is are no `category` or `view` directories to be causing conflicts. – morgant Oct 22 '09 at 22:30
  • 1
    Hm. Well without seeing all of httpd.conf I'm not sure what's rewriting the requests to `/category.php/$1` but I'm glad it's working now! I suspect that something sees a PHP script with that basename and rewrites. (That's why I suspected FastCGI PHP). Do the same rewrite change for `/view` and `/order` and you should be set. – Josh Oct 22 '09 at 22:40
0

I'm probably being an idiot here, but isn't that the expected behavior? Mod_rewrite changes the URL inline for finding the file, the environment variables seen by PHP don't normally change.

I think you have to parse the REQUEST_URI string to pull out the category variable.

Mark

mfarver
  • 2,576
  • 14
  • 16
  • $_GET isn't an environment variable -- it's a superglobal containing the result of query strings. What he's trying to do should work. – Josh Oct 22 '09 at 21:11
  • Hmm, I thought that because `RewriteRule ^category/([^/\.]+)/?$ /category.php?category=$1 [L]` included the substitution string `/category.php?category=$1` that it would outright replace the URI, even for internal redirects. [http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule] notes that "The URL is completely replaced by the Substitution and the rewriting process continues until all rules have been applied, or it is explicitly terminated by a L flag." Also, if I change the flag to `[R,L]` it exhibits the same behavior and doesn't redirect. – morgant Oct 22 '09 at 21:16
  • It should be working as you originally expected, although the [R] flag not taking effect is unusual. Can you enable the rewrite debug log? – Josh Oct 22 '09 at 21:19
  • **@mfarver** You are correct that `$_SERVER['REQUEST_URI']` comes through as `/category/2/` even after it's fixed, but the question was really about the rewrite not working and so `$_GET['category']` coming through blank. – morgant Oct 23 '09 at 00:25