5

The following rule in my .htaccess works just fine for routing calls to index.php:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

However, this does not work when running on a temporary URL like http://example.com/~test/.

In such a case, the .htaccess file needs to read:

RewriteEngine On
RewriteBase /~test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

Note the extra RewriteBase.

Question: is it possible to write a rule in .htaccess to "detect" whether to apply that RewriteBase?

The reason is the .htaccess if version controlled, and I'd like for it to work on multiple installations.

  • I've spent so long screwing around with this, and have found the same problem. Why the hell cant RewriteBase have the same value as Alias? It's crazy... – Nick Bolton Jun 8 '12 at 21:02
  • Yes, it seems like a pain that's just "one of those things". – eoinoc Jun 12 '12 at 22:10
1

As far as I know, there's no reliable way to detect what the RewriteBase should be. If there were, there would be no need for the kluge that RewriteBase is, as mod_rewrite could just detect the correct base automatically.

| improve this answer | |
4

Trying the same and on my search I found that symfony2 frameworks .htaccess file is doing that at least it is stated in the file that it would. I tried to use it in my own setup but didn't work. Maybe that will help you guys somehow.

# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
| improve this answer | |
0

...does not work when running on a temporary URL like http://example.com/~test/

The "problem" in this instance really stems from using (what appears to be) an "Apache per-user web directory". If /~test was a physical subdirectory then you would not need to explicitly set the RewriteBase, as the directory-preifx (the absolute filesystem path to the location of the .htaccess file) would be "correct". The issue here is that /~test does not relate directly to a physical subdirectory - it is a URL-path (as opposed to a filesystem path) and is essentially an alias to another part of the filesystem - and therefore is different to the directory-prefix.

By default, a relative substituion string in the RewriteRule directive is relative to the filesystem (the directory-prefix). But when using an Apache per-user web directory or Alias, it now needs to be relative to the URL-path. This is when the RewriteBase directive is required*1 - it enables relative substitution strings to be relative to the stated root-relative URL-path (as opposed to the default absolute filesystem path).

*1 Or, this can be calculated using a method as mentioned in @FitzPatrick's answer. This sets an environment variable that must be explicitly used in each substitution string as required. For example:

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule (.*) - [E=BASE:%1]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) %{ENV:BASE}/index.php [L]

But note that this does not behave quite the same as when using RewriteBase. When using RewriteBase, the URL-path is only prefixed at the very end of the rewriting process. This can be an important factor if you have rules that are expected to chain together. (And this also behaves differently to when no URL-path prefix is specified at all.)

Aside: "Temporary" per-user web directories can cause other issues with root-relative URLs in client-side development. Unless these are factored into account on the development server then root-relative URLs are going to be different between servers.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.