1

I'm facing an Apache configuration issue which can be summarized like follows.

  1. On a unique hosting system I have a lot of different test sites, each one in its own subdirectory, so they are accessible through an url like myhostname.fr/sitename.
    Hence in the corresponding .htaccess, the common practice is to have a RewriteBase /sitename before any of the RewriteCond+RewriteRule sets, and it works fine.
  2. Now for one of these sites (say in the specialsite subdirectory) I had to create a dedicated domain so the url looks like domainname.myhostname.fr.
    Then for this site to work the .htaccess now needs RewriteBase / instead of RewriteBase /specialsite, and it works fine too.
  3. Here is the trick: being not so familiar with Apache I decided to experiment and wanted to also keep allowed to access this site through the common url myhostname.fr/specialsite.

So I had to find a way to conditionally use one of the above RewriteBase, depending on which is the current url.

The first way I tried was to work like this:

<If "%(HTTP_HOST) =~ domainname\.myhostname\.fr"> RewriteBase / </If> <If "%(HTTP_HOST) =~ myhostname\.fr/specialsite"> RewriteBase /specialsite </If> 

But I got a HTTP 500 error, and I take much time to understand that the <If> directive is available as of Apache 2.4, while my hosting only offers Apache 1.3!

So (thanks to some other SO answers) I thinked to another way, which is to first do:

RewriteCond %{HTTP_HOST} domainname\.myhostname\.fr RewriteRule ^ - [E=VirtualRewriteBase:/] RewriteCond %{HTTP_HOST} myhostname\.fr/specialsite RewriteRule ^ - [E=VirtualRewriteBase:/specialsite/] 

Then prepend all further RewriteRule replacement with the given VirtualRewriteBase, like in this one:

RewriteRule ^ %{ENV:VirtualRewriteBase}index.php [L] 

But while it works fine for the domain-access version, it gives me an HTTP 404 error for the subdirectory-access version.
So in order to watch at how the replacement applied I changed the above rule for:

RewriteRule ^ %{ENV:VirtualRewriteBase}index.php [R,L] 

And I observed that the redirected url looked like this:

http://myhostname.fr/kunden/homepages/7/d265580839/htdocs/specialsite/index.php 

where kunden/homepages/7/d265580839/htdocs/ is the full document-root of my hosting.
You can notice that the document-root has been inserted between the two parts of the original url.
Moreover, the result is exactly the same whatever I put in place of /specialsite/ in my VirtualRewriteBase!

So here is my main question: why and how does this happen?
Also I'm obviously interested to a possible alternative solution to achieve the double-access availibility.
But above all I would like to understand...

1 Answer 1

1

But while it works fine for the domain-access version, it gives me an HTTP 404 error for the subdirectory-access version.

That's because your second condition is never matched. Indeed, HTTP_HOST only contains the... http host ! The /specialsite is part of the REQUEST_URI (or can also be matched in RewriteRule directly).

This code should work (anyway, i don't know if it would solve totally your problem, but that's a first step)

RewriteCond %{HTTP_HOST} ^domainname\.myhostname\.fr$ [NC] RewriteRule ^ - [E=VirtualRewriteBase:/] RewriteCond %{HTTP_HOST} ^myhostname\.fr$ [NC] RewriteCond %{REQUEST_URI} ^/specialsite(?:/|$) [NC] RewriteRule ^ - [E=VirtualRewriteBase:/specialsite/] 
Sign up to request clarification or add additional context in comments.

1 Comment

As you point out, "Indeed, HTTP_HOST only contains the... http host !". But this is just the issue: it wasn't indeed for me. I din't have a clear idea of where is the border between HTTP_HOST and REQUEST_URI in this peculiar case. So now it becomes clear... and hence simple! I tried it and it works fine. Thanks a lot for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.