htaccess redirect on specific subdomain - apache

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]

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.

How can I isolate addon domains under public_html from the main domain?

I am using Directnic to host multiple websites. Directnic, like many hosts, requires each website to be placed under the /home/<user>/public_html directory. And this works. My problem is that some websites are accessible from the "main" domain.
Here is an example of what my configuration looks like...
one.com is tied to the hosting plan, and is stored in public_html directly.
two.com is setup as an addon domain and is stored in public_html/two.
three.com is also setup as an addon domain and is stored in public_html/three.
This all works as expected, all three websites show three different websites and all have three different document roots, so everything works nicely.
My problem is that I can access the files for two.com and three.com by going to http://one.com/two/, etc, and I don't want this, I want them to be more isolated.
What configurations might I be able to make to hide these files from one.com?
My host uses Apache and cPanel, but I don't have access to specific conf files, so I can't create virtual hosts. Maybe some .htaccess trickery?
This is the way cPanel works. You cannot isolate it 100% and you will not get configuration access to add virtualhost entry. It is being managed by your service provider.
You can certainly restrict the access for HTTP. You can add rewrite rule in your subfoldess (in above example two and three folder). You can try the following .htaccess rewrite rules to prevent direct access from main domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addon1/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/addon2/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/addon3/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
Once you add above rule, your main domain should not be able to access addon folder usin g HTTP. It should show 404 error page.

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

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!

Apache Rewrite: secondary htaccess for domain specific RedirectMatch

On shared web-hosting my software supports multiple domains (all domains point to the same public_html root directory).
What I want to do is keep redirects (and any RedirectMatch) in their own host specific/dedicated .htaccess file.
Visually the directory structure looks like this...
/public_html/ (all domains are pointed internally to this directory)
/public_html/.htaccess
/public_html/www.example1.com/
/public_html/www.example2.com/
/public_html/www.example3.com/
There are two approaches I'm considering though would appreciate input from others:
The first would be to keep domain specific redirects out of the main .htaccess file as defined above. So I'd like to have redirects handled by the .htaccess files as defined by below if possible...
/public_html/www.example1.com/.htaccess
/public_html/www.example2.com/.htaccess
/public_html/www.example3.com/.htaccess
...if this is not feasible I'll settle for a rewrite to a PHP file to hand off redirects to PHP instead. I imagine this isn't as performance oriented though on the other hand it would give me the opportunity to log redirects and see how long it takes them to level off.
Some clarifications:
I'm using shared web hosting so anything Apache related needs to be done through .htaccess files only.
There are no redirects/matches in the master .htaccess file nor will there ever be since two domains may eventually attempt to use the same redirect.
Since you are on shared host, You cannot afford to have any solutions concerning conf files (which BTW are better). So wont bother to list them. Best way to do the above is like this:
The code was written keeping in mind that none of the domains share any kind of file/data on the server. Every file/data pertaining to a domain is kept under a folder having the name equal to its domainname.
The code below is tested(both static and non static):
RewritEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
And add either of the following to the above:
for doing it statically:
RewriteCond %{HTTP_HOST} ^www\.(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /www.%1%2/$1 [L]
for doing it statically: and also if you want to access the site without www
RewriteCond %{HTTP_HOST} ^(www\.)?(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /%1%2%3/$1 [L]
for Non-statically do it: this is a better sol
RewriteRule ^(.*)$ /%{HTTP_HOST}/$1 [L]
All the above will do is redirect URI to their specific domain's folder. All other domain specific rewrites can be handled in the respective folders.
If you have URIs without the www, i.e. example1.com change ^www\.(example1|example2|example3)(\.com)$ to ^(www\.)?(example1|example2|example3)(\.com)$

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/