I'm facing an Apache configuration issue which can be summarized like follows.
- 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 aRewriteBase /sitenamebefore any of theRewriteCond+RewriteRulesets, and it works fine. - Now for one of these sites (say in the
specialsitesubdirectory) I had to create a dedicated domain so the url looks likedomainname.myhostname.fr.
Then for this site to work the.htaccessnow needsRewriteBase /instead ofRewriteBase /specialsite, and it works fine too. - 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...