mod_rewrite 404 error

Hi,

I’m having some trouble getting mod_rewrite to work on my testing/development subdomain for a new project of mine.

I have a URL: subdomain.mydomain.com/dir1/dir2/file.php?var=value. All I want is to have it look like subdomain.mydomain.com/dir1/dir2/value.

I’ve tried everything I can think of, but to no avail. I’m sure that everything is configured correctly in my httpd.conf file since I have mod_rewrite working correctly for another site on the same server.

I’ve never had trouble with rewriting URLs in the past, is there some hidden trick when dealing with a subdomain that I don’t know about? I’m about to pull my hair out! :injured:

Create a .htaccess in de document root (where the index.php is) and put these contents in it:


RewriteEngine On
RewriteRule /?dir1/dir2/([a-zA-Z0-9]*) /dir1/dir2/file.php?value=$1

Should work IMHO (as long as value can only take characters from a-z, A-Z and 0-9, otherwise change [a-zA-Z0-9]) :slight_smile:

BB,

Yes, you need to put the .htaccess in the subdomain’s DocumentRoot (kind of what ScallioXTX was saying).

If you’re using Apache 2.x, you don’t need the regex’s leading /? and should have an END anchor ($). The redirection should not have the leading / and you do need a Last flag after the redirection.

More info (and examples) in my signature’s tutorial article.

Regards,

DK

Thanks for the replies guys, but I’m still getting that pesky 404 error.

I tried your code, Scallio. I also took a look at your tutorials (which are very informative btw), dklynn. I even used your little test procedure in my subdomain document root and it worked fine, indicating mod_rewrite is working.

I’m running Apache/1.3.41 and as seen by dklynn’s test and my phpinfo() file, mod_rewrite is up and running. When I go directly to file.php?var=value it works so I know it has nothing to do with the actual file either. Please tell me, what is wrong with the following code?


RewriteEngine on
RewriteRule ^/?dir1/dir2/([a-zA-Z0-9]*)$ dir1/dir2/index.php?ident=$1 [L]

BB,

Bummer! Okay, Apache 1.x makes a difference as you do need the leading / in your regex (if you’re going to use the start anchor, ^).


RewriteEngine on
RewriteRule ^/?dir1/dir2/([a-zA-Z0-9]*)$ dir1/dir2/index.php?ident=$1 [L]

Your code is just fine as ^/? is accepted by both Apache 1.x AND Apache 2.x so, if your host upgrades (as they should), you’ll be all set.

dir1/dir2/ are static directories so no problem (so long as your index.php script IS located in dir2).

The ([a-zA-Z0-9]*) atom is lowercase and uppercase letters and digits, zero or more, and is terminated by the end anchor - PERFECT (so long as your value is ONLY letters and digits - no example provided)!

The redirection is based on the current location (DocumentRoot) so the dir1/dir2 is fine as is the index.php. The ident key is then assigned the value captured by the atom in the regex and the mod_rewrite block is terminated (as in ; or }) by the Last flag.

At this point, I have to ask for the test URI that you are using for this redirection (and the target URI).

Regards,

DK

Wow, I fixed the problem. It turns out that there was a copy of the .htaccess file in my second directory and it was apparently messing everything up. As soon as I deleted it, everything works fine.

Thank you very much for the help, dklynn. Sorry for trying to rack your brain when it was my fault all along!

BB,

No worries. 'Glad you got it sorted out.

Regards,

DK