.htaccess - Redirect from subdomain root to subdomain's folder (mod_rewrite/mod_proxy) - apache

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

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]

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!

Related

htaccess Remove directory from end of URL in apache

Ok, so I know this is a question that has been asked many times, however, I have not been able to find an answer to my particular case, so please do not shoot me down.
I have a website: http://gmcomputers.co.za.
I am redirecting this URL, using .htaccess file, to a subfolder to load the content:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /gmcomputers/ [L,DPI,R=301]
Which works perefectly, except when I go to http://gmcomputers.co.za I get http://gmcomputers.co.za/gmcomputers/.
So my question is, how do I modify the above code to remove the /gmcomputers/ from being appended?
Please note I copied the code above from a website as I am not at all experienced in redirect, etc and am still learning. Also, the reason I am using .htaccess to redirect is due to there being other websites in the root directory and I therefore cannot edit any config files for Apache.
Thanking you.
You contradict yourself in your question. On the one hand you write that you want to redirect and that this "works perfectly", but then you write that you do not want that result.
My guess is that you actually do not want to redirect at all, but that instead you want to internally rewrite your requests to point to that server side folder. While the URL visible in the browser's URL bar does not show that folder. Is that what you are trying to ask?
If so take a look at this example:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
You might want to add an actual redirection to direct clients actually using the folder name in their requests:
RewriteEngine on
RewriteRule ^/?gmcomputers/(.*)$ /$1 [R=301,END]
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
Best is to implement such rules in the central http server's host configuration. If you do not have access to that you can instead use a distributed configuration file (typically called ".htaccess") located in the DOCUMENT_ROOT folder configured for the http host, if you enabled the consideration of such files in your host configuration . Though that comes with a number of disadvantages. Above implementation works likewise for both approaches.

.htaccess directory exemption

I have an Oxwall site in my root directory. It's preventing my subdomain from redirecting to a subdirectory. The directory is a DokuWiki. I am absolutely horrible with RegEx, fyi.
I tried adding a line to my .htaccess, but to no avail
RewriteRule ^modpod/ -
That failed to provide any change. The address [domain]/modpod/ was still handled with the rest of the .htaccess. If I attempt to use the subdomain, I get a 500 error.
Help?
Upon redoubling my search of SOF, and applying a little creativity to an unanswered question, I ended up with a solution.
RewriteCond %{REQUEST_URI} !modpod/.* [NC]
That was added to the beginning of all my Rewrite conditionals. Works perfectly.

htaccess redirect on specific subdomain

Ok, I am working on a large collaborative project with a handful of people, and we are using SVN. So we have subdomains for each person acting as a repo for each person. With that, we have started to addon a new feature, well a couple of us have, of which requires the files from the repo. Its not a trunk/branch type of thing either.
Anyway enough pretense, what I want to do is take sub1.domain.com and make it automatically use another folder outside of root as the default loading directory. So I am wondering if this is even possible to be specific with a subdomain and htaccess for this cause.
Yes you can:
RewriteCond %{HTTP_HOST} sub.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/folder/$1 [P]

Domain Redirects SEO relative to the root folder

I got two related problems in the site root.
First, both "domain.com" and "www.domain.com" works the same way. Is that a problem for SEO purposes? Some guy told to use a redirect. Should I do that? What redirect should I use?
Second, my visitors that visit the site over the "/" url get geolocated and redirect to a specfic page, it means, if the visitor access "www.domain.com" from the city a, he gets a redirect to "www.domain.com/a/", if the visitor came from city b, he gets a redirect to "www.domain.com/b/", etc... How should I work over that?
PS: Feel free to rename the question, I dont know how to name it properly.
Thx
First point answer:
Yes, you should redirect it. Otherwise you can run into the duplicated content issue. You can redirect using .htaccess and the code will be this:
RewriteEngine on
RewriteCond %{http_host} ^site\.com [nc]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,nc,L]
Second point answer:
You can use .htaccess also in this case. But for this question, please bear this discussion in mind (especially my answer).

Apache Rewrite/Redirect different for TLD vs directory

I've migrated the content of a site from one domain to another. I have .htaccess Rewrite set up successfully to redirect any request to the old domain to the same location on the new domain, which is working fine, using the following code:
RewriteEngine On
rewritecond %{http_host} ^roundeltable.com
rewriteRule ^(.*) http://wheelspin.tv/$1 [R=301,L]
However, I want to go a little further. When somebody simply requests the old TLD (http://roundeltable.com/) I want them to be redirected to a specific page within the new domain (http://wheelspin.tv/rt). If they make a request to any other location from the old domain (for example, http://roundeltable.com/about) I want them to be sent to that exact same place at the new domain (http://wheelspin.tv/about) the way they currently are now.
Is this possible? If so, how?
So I've found the solution to this issue, which ended up being provided by somebody from Media Temple's service department. Here's the entire code required in the .htaccess file to make this work:
RewriteEngine On
RewriteRule ^$ http://wheelspin.tv/rt/ [L]
RewriteRule ^(.*)$ http://wheelspin.tv/$1
Obviously substitute whatever domain you want to redirect to in place of mine, but that's the solution for my example. Thanks for the help!
If you want to see all the details on my solution, I have it here: christiaanconover.com/media-temple-service-rocks/