Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule doesn't work without [R]

         

arabesque

7:58 pm on Aug 27, 2010 (gmt 0)

10+ Year Member



I have an .htaccess file in directory /pchem/ with the following content:

RewriteEngine on

RewriteBase /pchem/
RewriteRule ^(\w+)$ index.php?page=$1 [R]


This works great; inputting /pchem/lala redirects to /pchem/index.php?page=lala exactly as it's supposed to and it loads the desired content. However, I'd like to hide the index.php bit from users, so I'd like to remove the [R] tag... but when I do, the rewrite/redirect fails and I get a 404 error.

Any suggestions on how to fix this? I didn't see this particular topic addressed recently in the forums, but if it has been, it'd be great if someone would point me toward the thread.

jdMorgan

8:09 pm on Aug 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why are you using RewriteBase /pchem ? Is your server configured in some non-standard fashion or is DocumentRoot incorrectly defined?

Are there any other rewrites or redirects in this .htaccess file, in any .htaccess files in directories above this one, or in any server config files?

Jim

arabesque

9:07 pm on Aug 27, 2010 (gmt 0)

10+ Year Member



Ah, good point. There aren't any other lines in my .htaccess file for this directory, and there aren't any .htaccess files in the directories above this one. The caveat is that I'm on a shared host, so I can't really get to the server config files (and even if I can view them, I certainly can't modify them).

As for the rewritebase question, if I leave out the RewriteBase line I get redirected to
http://www.(mydomain).com/home/users/web/(SomeIDNumber)/(mydomain)/pchem/index.php?page=lala
instead, and that doesn't load my file correctly either (whether or not the [R] is in the RewriteRule line). On my local machine (where I am running the most recent version of WampServer; only thing I've changed about the config files is to enable mod_rewrite in httpd.conf), I can leave out the RewriteBase line and things still work just fine.

For that matter, I don't have the [R] problem on my local machine either, so maybe this all is just a problem with my hosting service's server configuration... if so, I'll try to take it up with tech support, but their tech support is a little spotty sometimes, so I want to make sure there's nothing stupid that I'm doing wrong before I try that.

jdMorgan

9:39 pm on Aug 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Located in /pchem/.htaccess, the code should be:

RewriteEngine on
RewriteBase /
#
RewriteRule ^(\w+)$ /pchem/index.php?page=$1 [L]

assuming that you're on Apache 2.x where PCRE patterns will be processed correctly. Otherwise, change the rule itself to:

RewriteRule ^([a-z0-9_]+)$ /pchem/index.php?page=$1 [NC,L]

using the alternate character group with [NC] flag as an equivalent to "\w".

In either cases, the rule invokes an internal rewrite, and the rewritten index.php filepath should not be exposed to the client as a URL unless some other 'agent' --for example, a script or some server configuration-level code-- subsequently invokes an external redirect.

Jim