KBD

Keith Devens .com

Monday, October 13, 2008 Flag waving
The wheels turn slow, but when they do, they grind. – Micah
← Form tools (generators)Stupid programmer tricks, part 001 →

Daily link icon Wednesday, July 7, 2004

mod_rewrite, SCRIPT_NAME, and PATH_INFO

Argh, why isn't PATH_INFO set when using mod_rewrite? I have the following lines in my .htaccess file to make PHP pages be able to be served without the .php:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_FILENAME}.php [L]

If my page is /foo.php and I visit it as /foo/bar, I expect SCRIPT_NAME to be /foo and PATH_INFO to be /bar. PATH_INFO isn't set, but if I visit the page as foo.php/bar it is. Also, SCRIPT_NAME points to the actual file location. So, if I visit the page as /foo/bar, SCRIPT_NAME is set to /foo.php and not /foo.

The net result of all this is that there's no way to split out the actual script name you requested from the PATH_INFO.

My only alternatives are using MultiViews, which does work as expected (though MultiViews has gotta be overkill just for this), and hard-coding the base path to the script in the script. And, even though the mod_rewrite documentation says the variables SCRIPT_URL and SCRIPT_URI should be set, they aren't.

← Form tools (generators)Stupid programmer tricks, part 001 →

Comments XML gif

Ian Bicking (http://blog.ianbicking.org) wrote:

Did you try:

RewriteRule . %{REQUEST_FILENAME}.php%{PATH_INFO} [L]

∴ Ian Bicking | 8-Jul-2004 1:54am est | http://blog.ianbicking.org | #4946

Keith (http://keithdevens.com/) wrote:

Weird. No, but I'll give it a shot.

Keith | 8-Jul-2004 1:58am est | http://keithdevens.com/ | #4947

Keith (http://keithdevens.com/) wrote:

Whoa, that worked. Why did that work?

Now all I have to do is figure out how to set the SCRIPT_NAME to the right thing and I'm golden. Got any ideas about that? Smiley

Thanks for your help.

Keith | 8-Jul-2004 2:02am est | http://keithdevens.com/ | #4948

Keith (http://keithdevens.com/) wrote:

P.S. I realize that as long as I have the REQUEST_URI and the PATH_INFO I can derive the SCRIPT_NAME. I'm just wondering if there's any similar trick I can do in mod_rewrite with environment variables or something.

Keith | 8-Jul-2004 2:09am est | http://keithdevens.com/ | #4949

Keith (http://keithdevens.com/) wrote:

Whoa, that worked. Why did that work?

Ah, I get why that worked now. Makes sense. Thanks again.

Keith | 8-Jul-2004 2:14am est | http://keithdevens.com/ | #4950

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 7 posts

Recent comments XML

new⇒Spider solitaire

I have now won, at the "Difficult"​level, 186 games of Spider​Solitaire.  I...

75.179.28.113: Oct 13, 9:34am

new⇒Girls, please don't get breast implants

Please, don't marry him.

You​want to get surgery to make​yourself more i...

gigi: Oct 12, 11:47pm

I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Generated in about 0.222s.

(Used 8 db queries)

mobile phone