Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Apache's RewriteRule revealing local paths?

I'm trying to use RewriteRules in .htaccess with relative paths, but Apache seems to want to output the physical path instead of the server path whenever I try to output a relative path. Absolute and server-root paths work fine. For example:

RewriteEngine On

# this works fine, 127.0.0.1/ab redirects to 127.0.0.1/cd
RewriteRule ^ab$ /cd [R]

# this doesn't work... 127.0.0.1/wx redirects to 127.0.0.1/C:/path/to/files/yz
RewriteRule ^wx$ yz [R]

Adding a "RewriteBase /" solves the problem, but it's tedious to add the path to every .htaccess, and it makes it harder to change the directory structure. Is there a reason RewriteBase defaults to the current physical path instead of the current URI path?

like image 547
zildjohn01 Avatar asked Jul 22 '09 03:07

zildjohn01


People also ask

What is RewriteRule?

L|last. The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.

What is htaccess RewriteRule?

htaccess rewrite rule includes setting a combination of rewrite condition ( RewriteCond ) tests along with a corresponding rule ( RewriteRule ) if the prior conditions pass. In most cases, these rules should be placed at any point after the RewriteEngine on line in the . htaccess file located in the website's docroot.


2 Answers

For those who happen to arrive here from Google (like me), the short checklist:

  • Make sure you have RewriteBase / (or any other value - the statement is what is important)

  • If you use redirect ([R], [R=30x], etc) - make sure the new URI starts with a / and contains a path relative to your domain root

  • (If above didn't help yet) Restart Apache, clear your browser's cache (especially if you have used [R=301] at some point)

That's what saved my day, maybe it will save yours too.

like image 56
mkilmanas Avatar answered Sep 23 '22 02:09

mkilmanas


It's because of the [R] which means the server will redirect to the new path (so the user's browser will issue a new request with the newly sent uri) instead of translating internally the URI to a local path.

In your first RewriteRule, there is an slash in the new path, thus the server doesn't try to translate it to the local path, but in the second rule, there is no slash, this is why it redirects to a complete local path. This explains too why it works with the RewriteBase set.

Either remove the [R] (you can replace it by a [L] in your case, this avoids the server trying to match other rules once it found a matching one), or add a slash before "yz" in your second RewriteRule.

I'd suggest to simply replace the [R] with a [L]: this way, the user won't see the rewritten path, which is generally what RewriteRules intend to do (mainly for SEO purposes), unless you specifically want to redirect your users to a new URL.

like image 34
FWH Avatar answered Sep 26 '22 02:09

FWH