Problem
I am trying to redirect from the root folder of a sub-domain, e.g. sub.example.com
, to a sub-directory of the sub-domain and keep the sub-domain in the address bar, e.g. sub.example.com/subdir1
.
This is not the first time that I have used mod_rewrite
, but I am not an expert. I have not used mod_proxy
before, but I believe that it will be required for this task.
Background
This is part of a project that I am working on in which the web-host is private, but the server cannot be modified. I am required to specify all sub-domains using their tool, as opposed to creating the rewrites myself. This question pertains to a sub-domain being used as a development site, which will eventually go live without being a sub-domain, e.g. sub.example.com
will become sub.com
when it goes live. The reason why I cannot simply set the root folder of the subdomain to the directory that I am redirecting to is that the code base for the project relies heavily on the DOCUMENT_ROOT
for internal link management, and the target folder, sub.example.com/subdir1
, is not the root of the site.
What I have done
I will start by saying that I have exceeded my allotted time to work on this problem, mainly out of interest.
I have done a lot of research and have tried to pull bits and pieces from the sources that I have looked at. However, all but one of the sources that I have looked at only had information for redirecting from a subdomain to a subdirectory of the site's root, e.g. sub.example.com
-> example.com/root-subdir
. In fact, the only reason that the aforementioned page did have information related to my question is because the user who asked the question accidentally did what I am currently trying to do. Unfortunately, what he did still is not working for me. Here is what I am trying right now (but is giving me a "310 - too many redirects" error):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com
RewriteRule ^ /subdir1/ [R=301,L]
As previously stated, I think that I may need to use mod_proxy
for this, but I have no idea what that would look like.
Any help that you can give would be much appreciated. Thank you for your help! :)
The solution given in the comment by @faa did not completely solve my issue, as my real mistake was forgetting to match the root, and only the root, of the subdomain. @faa did, however set me back onto the right track and did answer my question regarding preventing loops. The information that I gained from using his snippet led me to realize what I needed, which I found in the solution to this ServerFault question.
My final solution is the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subdir1/ [R=301,L]
Thank you all for your help!