Apache (htaccess) redirect to directory based on user selected domain - apache

I found some interesting thoughts here on stackoverflow, but none is stated clearly enough for me, so I have to ask myself if it even could be done :)
Situation:
I have linux with apache http server installed and a bunch of sites in folders in www directory with different contents.
Let's say:
www/Site1 (Phpbb)
www/Site2 (Wordpress)
www/Site3 (Own web pages)
I own one domain intended for use on Site3 (let's call it site3.com, www.site3.com).
I have created two more domains at no-ip.org (let's call them site1.sytes.net and site2.sytes.net).
All three created domains are pointed to port 80 of linux (apache) servers ip address.
So we have these domains ready:
www.site3.com
site1.sytes.net
site2.sytes.net
Now the question:
How to create redirects to particular directories for users who want content from those domains.
I want user who want to get "site1.sytes.net" (and wrote that to browser) to be pointed to Site1 directory and rewrite browser address to omit that directory and put just "http://site1.sytes.net".
For example:
User want: site1.sytes.net [/index.html]
Server goes to: www/Site1 [/index.html]
User see in browser: http://site1.sytes.net [/index.html]
And so on:
site2.sytes.net [/index.html] => www/Site2 [/index.html]
www.site3.com [/index.html] => www/Site3 [/index.html]
If I am not mistaken HTTP_HOST will return just domain, so it is useless for trying to find out what user wrote to browser for site1.sytes.net and site2.sytes.net (because it will return just sytes.net).

The %{HTTP_HOST} is indeed what you want, and yes, it only matches the domain. But you use it in a condition and you use either %{REQUEST_URI} or the rule itself to match against the URI. Try putting this in your htaccess file in your document root (which I assume is the same for all your sites, otherwise, there's no point in doing any of this):
RewriteEngine On
RewriteCond %{HTTP_HOST} site1.sytes.net [NC]
RewriteCond %{REQUEST_URI} !^/Site1
RewriteRule ^(.*)$ /Site1/$1 [L]
RewriteCond %{HTTP_HOST} site2.sytes.net [NC]
RewriteCond %{REQUEST_URI} !^/Site2
RewriteRule ^(.*)$ /Site2/$1 [L]
RewriteCond %{HTTP_HOST} www.site3.com [NC]
RewriteCond %{REQUEST_URI} !^/Site3
RewriteRule ^(.*)$ /Site3/$1 [L]

Related

Why is the htaccess in my subdomain folder overriding aspects of the htaccess in my main domain?

Let me explain my setup here, I have two domains, for the sake of naming them lets call them domain1.com and domain2.com. I have shared hosting that runs cPanel.
Domain1.com is my main domain, and is what I have my hosting account setup using. I have a website hosted at that domain. Domain2.com is set as an add-on domain, and directs to a folder inside of the main domains root. That has it's own website (a Ghost blog).
That all works fine. However the websites hosted at the two domains are quite different and I did not want the subdomain to work, but as far as I can tell add-on domains in cPanel have to have a subdomain in order to be added to the account. The redirect options for the subdomains in cPanel aren't good enough for me, as I wanted anyone accessing say domain1.com/domain2 to get a 404 error as if it didn't exist. So I set up the .htaccess file for domain1.com to look like this:
RewriteEngine On
#301 (permenant) redirects all HTTP requests to HTTPS (SSL)
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#404 redirects all domain.com/subdomain requests
RewriteCond %{HTTP_HOST} ^(www.)?domain1.com$ [NC]
RewriteCond %{REQUEST_URI} ^/domain2/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
#404 redirects all subdomain.domain.com & www.subdomain.domain.com requests
RewriteCond %{HTTP_HOST} ^domain2.domain1.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain2.domain1.co.uk$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ - [L,R=404]
This worked perfectly, anyone tryting to access domain2 via domain1 would get a 404 error. But that was before I installed Ghost on domain2 After getting Ghost installed I created an .htaccess file in the domain2 folder inside of domain1's root.I had to put this in it in order for Ghost for to run, as it uses NodeJS:
RewriteEngine on
RewriteRule ^(.*)$ http://127.0.0.1:55555555/$1 [P,L]
This also works perfectly except for one aspect. Now all requests to the domain using domain1.com have started working again. For example if I type domain2.domain1.com it will now show me the Ghost blog where as before it was correctly displaying a 404 Not Found error. Why is this and how do I go about rectifying this issue?
As a side note, I also started trying to have all http requests redirect to https. I added the following to the domain1.com htaccess file as I wanted all requests on both domains to redirect to https and I assumed this was necessary:
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Now this kind of works. All requests, whatever they are (with or without www for instance), redirect to https, and when I visit it in my browser I get a green the icon indicating it's secured with SSL. Again even with this the previous issue of the domain redirect still doesn't work. But I have another odd issue here.
If I type in my browser simply "domain1.com" (ie without https:// or www. or a combination of the two), it will show just "domain1.com" in the address bar but with the green icon and say it is secured. If however I type in "domain2.com" (again with no https:// or www etc.) it does the same thing except it states it is not secure, suggesting it requested http.
And yet if I type in www.domain2.com or http://domain2.com, that works and I get the secured icon. It is highly puzzling. It seems that the https redirect works on every request except for just "domain2.com" while any other variation (www.domain2.com, http://domain2.com etc) works fine. Any clues? I mean clearly I've done something wrong with the htaccess files but I don't know what, I got most of what I'd put in them from searching sites like Stackexchange, but personally I don't really know anything about them or how they work.
I think I have fixed my own problem here.
I corrected the main htaccess so the https redirect acts like this;
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
From what I can tell it's better to do it this way than the way I had it previously. This in itself didn't fix the problems. What fixed it for me is editing the domain2.com htaccess to look like this:
RewriteEngine on
#Force WWW if it isn't in the request
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
#Or include WWW even if it is in the request
RewriteCond %{HTTP_HOST} ^www\. [NC]
#Then Force it to use https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Defines this domain so main htaccess rules for subdomains work
RewriteCond %{HTTP_HOST} ^(.*)?domain2.com$ [NC]
#Proxy for Ghost
RewriteRule ^(.*)$ http://127.0.0.1:55555555/$1 [P,L]
Now everything works. All requests are redirected to https. All requests to the subdomain from the main domain fail with a 404. domain2.com now redirects to https://www.domain2.com or https://domain2.com so it is now always secured by SSL. I'm a novice at htaccess as is probably evident and I've no doubt some or all of this could have unnecessary elements removed or simplified so if anyone wants to correct my syntax so it's less messy I'd appreciate it.

.htaccess redirecting URL but not content for subdomain

I'm trying to set up a test site but having real trouble getting .htaccess to redirect properly.
I want the contents of www.example.com/test to show when a user types in test.example.com. My rewrite rules allow me to use test.example.com in the address bar, but it's actually showing me the content of the root (www.example.com), not the test subfolder.
I'm not an .htaccess guru by any stretch, but I've been using Stack Overflow for 5 years and this is the first time I've been stumped enough to ask a question! Your collective wisdom is appreciated.
Here's the relevant part of my .htaccess code:
RewriteEngine On
# Rewrite for http cases
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
# Rewrite for no www cases
RewriteCond %{HTTP_HOST} !www\.example\.com [NC]
#redirect for test subdomain
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# redirect to correct for old subfolder usage
RewriteRule ^oldsubfolder/$ https://www.example.com/ [L,R=301]
I want the contents of www.example.com/test to show when a user types in test.site.com.
I assume you just have one domain and test.site.com should really be test.example.com (which would seem to be consistent with the rest of your question)?
In the code you've posted, there's nothing that really attempts to do this redirect? In the code you've posted, a request for test.example.com would not be redirected - so if it is then you may be seeing a cached response. Clear your browser cache.
You would need something like:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(test)\.example\.com [NC]
RewriteRule (.*) http://www.example.com/%1/$1 [R,L]
The (?:www\.)? part simply catches an optional www subdomain of the subdomain! Depending on how this subdomain was created, both test.example.com and www.test.example.com might be accessible. (Although I suspect your SSL cert probably doesn't allow this anyway?)
%1 is a backreference to the captured group in the CondPattern (ie. test) and $1 is a backreference to the captured RewriteRule pattern. Capturing the subdomain (eg. "test") just avoids repetition, but also allows for more than one subdomain to be handled by the same rule.
This is also a temporary (302) redirect. Change this to a 301 only when you are sure it's working (if that is the intention). 301s are cached by default, so can make testing problematic.
Clear your browser cache before testing.
# Rewrite for no www cases
RewriteCond %{HTTP_HOST} !www\.example\.com [NC]
#redirect for test subdomain
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
The comment in the middle of this block would seem to be misleading (it doesn't "redirect for test subdomain"). The whole block just redirects to www, excluding the test subdomain. The other code then redirects the subdomain.
UPDATE:
I was hoping it would continue to show test.example.com in the address bar
Yes, this is possible. Providing test.example.com and www.example.com point to the same filesystem then you can simply rewrite the request without actually changing the host. For this example, I'll assume test.example.com and www.example.com point to the same document root.
Change the above redirect to the following rewrite:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(test)\.example\.com [NC]
RewriteRule (.*) /%1/$1 [L]
The request now stays on test.example.com and will serve content from test.example.com/test (although this is hidden from the user) since test.example.com and www.example.com are really the same thing.
The check against REDIRECT_STATUS ensures we are only processing the intial request and not the rewritten request, thus avoiding a rewrite loop. REDIRECT_STATUS is empty on the initial request and set to 200 after the first successful rewrite.
However, if test.example.com points somewhere entirely different then you'll need to implement a reverse proxy and "proxy" the request to www.example.com in order to "hide" this from the user.

Redirection between domains

I have two webs on two different domains. One is on domain.com and the other web is on domain.es.
I am shutting down the web on domain.es but first I have to redirect every request to domain.es to domain.com. This is easy and I had already done it with RewriteCond & RewriteRule, like this:
www.domain.es/ -- redirects to --> www.domain.com/
But I am having a problem now. I also need to redirect one specific page from domain.es to domain.com like this:
www.domain.es/a_page/ -- redirects to --> www.domain.com/another_page/
I am having problems when redirecting with both rules, as when apache detects a request to www.domain.es/a_page it goes to www.domain.com/ and not to the specific page I need.
I have this in my virtualhost:
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
RewriteRule ^(.*)& http://www.domain.com/another_page/ [R=301,L]
RewriteCond %{HTTP_HOST} ^domain.es/
RewriteRule ^(.*)& http://www.domain.com/ [R=301,L]
I've got the [L] flag so apache would stop processing rules, but this is not working.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
The line above is never going to match anything - you're asking if the host is equal to domain.es/a_page/, which is a host as well as a path. You need to test against only the hostname, and then redirect based on the path, e.g.
RewriteCond %{HTTP_HOST} ^domain\.es
RewriteRule ^a_page/ http://www.domain.com/another_page/ [R=301,L]

redirect url from Domain A to another but applications exist on Domain A only

I have requirement on OHS server(apache) to redirect all requests from domain A to domain B but the pages needs to be rendered from domain A only. Below configuration doesn't seems to work.
RewriteCond %{HTTP_HOST} ^http://portal.mycompany.com$
RewriteRule ^(.*)$ http://newportal.mycompany.com:85/$1 [R=301,L]
Am i doing anything wrong?
For domain redirects could you simply point your new domain B to A using either CNAME or A records in the domain registrar?
There are several issues. You can't do a 301 redirect and expect to stay on Domain A. One way you might be able to get this to work is the use of mod_proxy and the P flag. Also the http_host variable only matches the hostname.
You need to make sure mod_proxy is enabled and you can try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^portal.mycompany.com$
RewriteRule ^(.*)$ http://newportal.mycompany.com:85/$1 [P]
In this way your domain A is sending a redirect to user browser, so the page is going to be provided from domain B.
If you have to make domain A providing pages from domain B, but user do not have to see domain B, you may check Proxy and/or ProxyPass rule from mod_proxy.
One more thing, if you proxy domain B thru domain A, you may have to rewrite inpage links, so take a look at ProxyHTMLEnable and ProxyHTMLURLMap
Dot (.) is a special character in the RewriteCond pattern, Escape it using a backslash
Try this
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.portal\.mycompany\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ http://newportal.mycompany.com:85/$1 [R=301,QSA,NC,L]

How do I redirect two domains to another domain with Apache?

I have three domains, two of which are supposed to be redirected to the other.
www.example.com
www.example.net
www.example.org
I already have the DNS entries setup so that they all will go to the same IP address.
What I want to have happen is for the .com and .net urls to be permanently redirected to the .org address. So:
http://www.example.com -> http://www.example.org
http://www.example.net -> http://www.example.org
http://example.com -> http://www.example.org
http://example.net -> http://www.example.org
In my .htaccess file I have the following configuration which I setup from the best of my understanding of http://httpd.apache.org/docs/2.2/rewrite/remapping.html#canonicalhost
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*)$ http://www.example.org/$1 [L,R=301]
Theoretically, what should happen is that any requests to the site where the HTTP_HOST is not www.example.org, then it should be permenantly redirected to http://www.example.org/ followed by any original path that was on the URL.
I'm sure this is easy to do and I'm just missing something obvious, but it seems like all of the other questions and search results talk about redirecting subdomains and file paths, but none of them talk about redirecting a top level domain in a URL.
Thats almost the same that I use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$
RewriteRule ^/(.*)$ http://www.example.org/$1 [R=301,L]
Turns out I was on the right track. My final code wound up being this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$ [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [L,R=301]
The root of the problem for me was that my host was unaware I had multiple domains. So when requests would come into the site after being resolved, the host would throw up a page saying there it couldn't find a site. So I added my example.com and example.net sites to my host and parked them to example.org.
Perhaps somebody else can better explain what happened here than I, but the real issue was not with the rewrite but with my hosting provider.