Mod_rewrite not behaving


#1

I’ve just migrated to DreamHost, and I’m having a problem with my mod_rewrite config on one of my sites. When I moved my site over, I moved my exact config over, and it’s not behaving the way it did at my old host.

The text of my .htaccess:

RewriteEngine On RewriteBase / RewriteRule ^/(content/.*) /$1 [L] RewriteRule ^/(includes/.*) /$1 [L] RewriteRule ^/(images/.*) /$1 [L] RewriteRule ^/(favicon.ico) /$1 [L] RewriteRule ^/(robots.txt) /$1 [L] RewriteRule ^/(.*) /index.php?$1 [L]

The goal here is pretty simple: if the requested URL is in the /content/, /images/, or /includes/ directories, or if it is a request for /favicon.ico or /robots.txt, let it through normally. Otherwise, redirect it to /index.php with the URL as a parameter to the php script.

Again, this worked fine (for well over a year) at my old host, and yet here on DreamHost, that last rule appears to be ignored entirely. Requests, for example, for “/about.html” result in a 404, implying that mod_rewrite didn’t even try to do anything with it.

Any ideas?


#2

I don’t know why it worked at your old host. Did you have these rules in the server config instead of .htaccess?

RewriteRule ^/(content/.*) /$1 [L]That is bad form for an .htaccess file placed in the document root.

RewriteRule ^(content/.*) /$1 [L]There we go, removed the leading slash. Should work now.

And yeah, this is documented on the Apache web site:

“Note: Never forget that Pattern is applied to a complete URL in per-server configuration files. But in per-directory configuration files, the per-directory prefix (which always is the same for a specific directory!) is automatically removed for the pattern matching and automatically added after the substitution has been done. This feature is essential for many sorts of rewriting, because without this prefix stripping you have to match the parent directory which is not always possible.”

:cool: [color=#6600CC]Atropos[/color] | openvein.org


#3

At the old host, yes, these were in the server config (the old host was a UML setup)

Oddly enough, though, if I change that particular rule as per what you’ve said, going to /content/ gives a 500 Internal Server Error. Removing the preceding / on any of the rules, in fact, causes that rule to result in such an error when it would have been applied to a request.

I guess it’s time to re-read the mod_rewrite documentation and see if that gets me anywhere.


#4

Yeah, for example

“There is a special substitution string named ‘-’ which means: NO substitution!”

which means you should try the following instead:RewriteRule ^content/ - [L]This works for me. Capturing the whole string and performing substitution is not necessary for what you want done in this case.

A total re-write then:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^content/ - [L] RewriteRule ^includes/ - [L] RewriteRule ^images/ - [L] RewriteRule ^favicon.ico$ - [L] RewriteRule ^robots.txt$ - [L] RewriteRule ^(.*) index.php?$1 [L] </IfModule>And also because you have RewriteBase /, you need to remove the leading slash from the substitution string.

:cool: [color=#6600CC]Atropos[/color] | openvein.org