0

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! :)

Community
  • 1
  • 1
Zachary Kniebel
  • 4,686
  • 3
  • 29
  • 53

2 Answers2

2

Now if you want to redirect just sub.example.com into sub.example.com/subdir1, then give this a try:

RewriteCond %{HTTP_HOST} ^sub.example.com
RewriteRule ^/?$ /subdir1 [R=301]
  • that is a solution that I have tried - unfortunately, the requirement that I specify the sub-domain in the server's tool prevents me from updating the `RewriteRule` for that redirection, and this solution to infinitely append `/root-of-sub/subdir1` to the end of my URL. If this is, in fact, the solution, then I will need a rule to prevent the redirection if the requested URL has already been re-written to `sub.example.com/subdir1`. Unfortunately, none of the conditions that I tried worked. – Zachary Kniebel Apr 04 '13 at 17:55
  • What? Could you please list all the URLs that are already rewritten into `sub.example.com/subdir1` and what dynamic URLs that you've wanted to be rewritten, and the URLs that you want to copy? – 5ervant - techintel.github.io Apr 04 '13 at 18:20
  • There is only one URL currently being redirected: `example.com/root-of-sub` -> `sub.example.com` (this is the redirect that I have no control over). Now I wish to add the following redirect: `sub.example.com` -> `sub.example.com/subdir1`. – Zachary Kniebel Apr 04 '13 at 18:26
  • Do you mean, you want to redirect just a single file? Check my updates above! – 5ervant - techintel.github.io Apr 04 '13 at 18:36
-1

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!

Community
  • 1
  • 1
Zachary Kniebel
  • 4,686
  • 3
  • 29
  • 53