Forum Moderators: phranque

Message Too Old, No Replies

Mod_rewrite problem

Parameters are not being passed

         

khjart

7:11 am on Jul 14, 2004 (gmt 0)

10+ Year Member



I am currently doing some testing on my private server with URL rewriting. I have a .htaccess file and a PHP script.

My .htaccess file:


RewriteEngine on
RewriteRule test/([0-9]+) script.php?var=$1

My script.php goes like this:


<?
echo $_GET['var'];
?>

But whenever I run
localhost/test/[any number]
it outputs nothing. It seems that the variable isn't passed at all.

Does someone know what could be wrong here?

gergoe

9:21 am on Jul 14, 2004 (gmt 0)

10+ Year Member



If you call the test.php straight with the var parameter is it working well?

khjart

11:23 am on Jul 14, 2004 (gmt 0)

10+ Year Member



Yes, that works. I suspect there might be something wrong with my config, but I have no clue what.

jdMorgan

2:42 pm on Jul 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



khjart,

Welcome to WebmasterWorld [webmasterworld.com]!

Are you sure the mod_rewrite code is even running? You may need to add


Options +FollowSymLinks

ahead of your RewriteEngine on directive, in order to enable mod_rewrite. If this is the case, your error log will usually say so.

Jim

khjart

1:16 am on Jul 15, 2004 (gmt 0)

10+ Year Member



Yes,
FollowSymLinks
is enabled in the config.
Apache also tells me that mod_rewrite is enabled.

khjart

1:43 am on Jul 15, 2004 (gmt 0)

10+ Year Member



Ok. I've found out that it works when I call
localhost/test/test/[any number]
So, this means I have to write test twice. Weird I think.

Edit: Now it works with
localhost/test/[any number]
I removed a comment line before the actual line from the .htaccess file. Does comments in the .htaccess file have any impact on the result?

jdMorgan

5:20 am on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Does comments in the .htaccess file have any impact on the result?

No, as long as the comment is on a separate line and starts with "#". Comments placed on the same line after a directive will cause Warnings (errors), which will appear in your error log if your error logging level is set to show warnings.

If you want to always access script.php in a fixed location relative to the Web root directory, then you should specify that by prefixing the substitution URL-path with a slash:


RewriteEngine on
RewriteRule test/([0-9]+) [b]/s[/b]cript.php?var=$1 [L]

You should also start- and end-anchor your RewriteRule regex pattern if possible, and use the [L] flag on the end of your rule unless you need mod_rewrite to process this URL further after this Rule is invoked -- In other words, if, after this rewrite is performed, you want to rewrite this URL some more, then don't use [L]. In practice, most rules should use the [L] flag.

Refs:
Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular Expressions Tutorial [etext.lib.virginia.edu]

Jim

gergoe

4:51 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



Where do you ahve the .htaccess file? In the root or a directory called test?

khjart

9:48 pm on Jul 18, 2004 (gmt 0)

10+ Year Member



Allright, thanks for your help guys.
gergoe: Yeah, I have it in the root.