Forum Moderators: phranque

Message Too Old, No Replies

How to start Mod Rewrite

         

Sadu

10:48 pm on Apr 1, 2005 (gmt 0)



I would call myself an fairly experienced PHP developer, and it's about time I started using Mod Rewrite to make my code more search engine friendly.

Except I can't get Mod Rewrite to work on any of my test machines.

Windows XP SP1, Apache 1.3.x and PHP 4.x

Apache's httpd.conf has the following line uncommented
LoadModule rewrite_module modules/mod_rewrite.so

I set up a file as such...
c:\htdocs\.htaccess with the following content

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^foo.htm foo.php

c:\htdocs has a file called foo.php, the .htaccess file and nothing else.

My expectation is that I can enter [localhost...] into my browser, and have it display content the same as [localhost...]

Instead I get a 404 error.

I have been through several Mod Rewrite tutorial sites, which indicate I'm doing everything right. Does anyone know what might be causing the problem? Any help greatly appreciated.

sitz

12:42 am on Apr 2, 2005 (gmt 0)

10+ Year Member



Apache 1.3 will also need the AddModule line for mod_rewrite uncommented for mod_rewrite to be active; I'll assume it is, but you should double-check.

Next, if you have write access to httpd.conf, that's really the preferred place for RewriteRule lines; the performance will be MUCH better. That's not always an option, but you should be aware.

As a matter of syntax, the RewriteRule should have a '[L]' at the end:


RewriteRule ^foo.htm$ foo.php [L]

at the end if you want mod_rewrite to stop processing the current request at that point; it's not *required*, but it's a good idea.

mod_rewrite requires that the 'FileInfo' option be active for the AllowOverride line which pertains to the directory containing the .htaccess file; in this case, you should have a line something like:


<Directory C:\htdocs>

in your httpd.conf; between it and the '</Directory>' line there should be a line which looks like this:

AllowOverride none

You'll need to change 'none' to 'FileInfo' if you want mod_rewrite to work, and you'll need to add 'Options' to that line as well, if you want the the 'Options' line to work in your .htaccess file. Note that I generally don't recommend enabling the 'Options' line on multi-user/multi-site systems, as they can allow untrusted users to do things you may not want them to.

If all of the above are true, your next step is likely to enable mod_rewrite's logging (in your httpd.conf) like so:


RewriteLog /path/to/rewrite.log
RewriteLogLevel 9

...and bouncing Apache. mod_rewrite will dump *copious* amounts of output to that log; it's generally not recommended that an admin enable the RewriteLog above Level 2 or so if the system does any significant traffic. If you get as far as needing to enable the logs *and* the logs don't help you, paste the results here, and we'll help you decipher them.

Good luck. =)

Sadu

1:22 am on Apr 2, 2005 (gmt 0)



Thanks for the quick response.

Yes, the other AddModule line is uncommented in Apache 1.3

When I change AllowOverride none to AllowOverride All or AllowOverride FileInfo, I get a 500 error instead of a 404.

The .htaccess file I have works fine on my webhost (Apache/Linux), but it's not working on my local windows test server. I'm aware the file isn't "best practice", I deliberately kept it simple to start with just to get it working. (but thanks for the tips - very useful to know)

Here is a snippet from httpd.conf - it's default apart from AllowOverride.

<Directory "C:/htdocs">

#
# This may also be "None", "All", or any combination of "Indexes",
# "Includes", "FollowSymLinks", "ExecCGI", or "MultiViews".
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
Options Indexes FollowSymLinks MultiViews

#
# This controls which options the .htaccess files in directories can
# override. Can also be "All", or any combination of "Options", "FileInfo",
# "AuthConfig", and "Limit"
#
AllowOverride FileInfo

#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>

sitz

5:24 pm on Apr 2, 2005 (gmt 0)

10+ Year Member



When I change AllowOverride none to AllowOverride All or AllowOverride FileInfo, I get a 500 error instead of a 404.

So, two things. 'All' sets 'Options', so the caveat I posted above regarding that applies. Second, if you're getting a '500' error, it likely means the .htaccess file is configured incorrectly; syntax errors or the use of directives in .htaccess which aren't available to it can cause that error. Apache should log the precise reason in its ErrorLog.