Simple URL Re-Write Rule Not Working

Hello,

I am trying to re-write this:

http://www.domain.com/sample/comments/68

Into this:

http://www.domain.com/sample/comments/index.php?submissionid=68

Here is what the .htaccess file looks like in both the directory represented by “sample” above and the directory “comments”:

RewriteEngine on
RewriteRule ^comments/([0-9]+)?$ index.php?submissionid=$1 [NC,L]

The first URL (http://www.domain.com/sample/comments/68) goes to a 404 error. I have checked, and mod_rewrite appears to be enabled. Also, when I manually enter the second URL, the correct page pulls up.

Any idea why my the URL re-write is not working?

Thanks in advance,

John

Zonie John,

/sample/comments/68 => /sample/comments/index.php?submissionid=68 is pretty easy BECAUSE the information is all there (and the directory level doesn’t change). The one thing that you may be caught on is that the subdirectory level mod_rewrite treats the / differently than the DocumentRoot. Therefore, Mitt’s suggestion is one EXCELLENT way to handle that. My recommendation, though, is to move your code TO the DocumentRoot (I collect all my mod_rewrite code there to avoid this leading / problem AND to see all the code at once - there often are conflicts).

RewriteEngine on
RewriteRule ^[COLOR="Gray"]sample[/COLOR]/[COLOR="Purple"]?[/COLOR]comments/([0-9]+)?$ index.php?submissionid=$1 [[COLOR="Red"]NC,[/COLOR]L]

… should get the job done (using the gray if in DocumentRoot or making the leading / optional with the purple ‘?’.

BTW, NC is No Case which is totally inappropriate to use on a URI. URIs are case sensitive; {HTTP_HOST} strings are NOT case sensitive so reserve your NC flags for use there and you’ll stay out of trouble.

If mod_rewrite “appears” to be enabled, use the test I provide in my signature’s tutorial article to be sure. Otherwise, you may just be spinning your wheels.

Regards,

DK

The caret - “^” - signifies “beginning with” (after the domain “root”).

So /sample/comments/68 does not match the rule.

Depending on what version of Apache you have you may or may not need the starting slash. But AFAIK if you use /? you should be good either way.

The only other thing I can think of that might be the problem is you may have wanted to specify the “base”.