Apache VirtualHosts - apache

If apache was presented with the domain name origin.datingasia.co would it match both below VirtualHost entries?
<VirtualHost *:80>
ServerName datingasia.co
ServerAlias www.datingasia.co
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/datingasia.co
ServerName origin.datingasia.co
ServerAlias origin.datingasia.co
</VirtualHost>
Also once a domain has the rewrite rule applied - eg: has 'http://www.etc.etct' added to it does it redirect automatically? would this occur before a DocumentRoot path would be used?

Only the second entry would be triggered for the domain "origin.datingasia.co". If you wanted the first one to trigger it as well, you would need to add a second ServerAlias parameter.
ServerAlias origin.datingasia.co
This would cause a problem though since your second entry contains that same ServerAlias. All ServerName/ServerAlias must be unique, otherwise Apache will not know which block to use for the request.
The first entry will only catch requests for "datingasia.co" and "www.datingasia.co". You're missing the DocuemntRoot though, in the event that the rewrite rule condition is not met (IE: www.datingasia.co). This will cause your requests to fail once they get to the www.datingasia.co page because Apache will not know what root to serve the requests from. The browser will automatically be redirected to "www.datingasia.co" when they visit "datingasia.co".
For the second entry, you do not need "ServerAlias origin.datingasia.co" since you already have that domain defined in the ServerName. You would only need a ServerAlias line if you wanted an additional unique domain to point to this host (IE: ServerAlias www.origin.datingasia.co).
Since your rewrite rules are in the host, if the condition is met Apache will not need the DocumentRoot. But if the condition is not met, Apache will attempt to serve the request and as a result will require the DocumentRoot. Your rule will forward "datingasia.co" to "www.datingasia.co" but will fail at "www.datingasia.co" because it is missing the DocumentRoot and does not meet the RewriteCond.

Related

Apache Configuration: redirecting to www in .htaccess

I have read a number of formulas for redirecting example.com to www.example.com and this appears to apply to .htaccess. However, I am confused about how this might work.
I want to assume that I have no access to the Apache vhosts configuration, and that I need to do it with .htaccess.
Suppose the configuration contains something like this:
<VirtualHost *:80>
ServerName www.example.com:80
ServerAlias www.example.com
VirtualDocumentRoot /whatever/example.com/www
</VirtualHost>
One such formula is something like this"
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ www\.%{HTTP_HOST}/$1 [R=301,L]
However, I don’t see how one root directory can respond to both requests.
The question is: Is it even possible to redirect example.com to www.example.com using .htaccess only, without tweaking the Virtual Host file?
The way I see it, the original request
I run my own server, and I can do anything I like. However, I am asking this because many of my clients and students have no access to tehe configuration, and can only fiddle with .htaccess.

Set urls for redirect to a specific url without rewrite

I'm building a webscheduler that have a multitenant structure, what I'm trying to do is assign a custom url that point to my application to each buyer.
So essentially when a user buy a license from me, I'll create a custom url on my webserver like this:
http://webserver/foo.scheduler.com/login
where foo is the name of the user that has buyed the license, and scheduler is a default part of the url, another example with more buyers:
http://webserver/foo.scheduler.com/login
http://webserver/foo2.scheduler.com/login
http://webserver/foo3.scheduler.com/login
essentially there are three buyers (my customers), each custom endpoint allow me to identify the correct database credentials, 'cause in my logic each tenant have a specific db, for more data organization.
Actually my application is located to this endpoint:
http://webserver/scheduler
I want to know if is possible point all custom urls to http://webserver/scheduler, without rewrite the url in the browser, so for example when the user go to http://webserver/foo.scheduler.com/login in the true is http://webserver/scheduler/login, but the user still continue to see http://webserver/foo.scheduler.com/login.
How can do this? In my .htaccess, available inside the root of the application folder I've this content:
RewriteEngine On
RewriteBase /webscheduler/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
this allow me to rewrite the base path to the index, and shunt the trace to the specific controller.
Happy to help you with this.
Get a valid SSL certificate for *.scheduler.com. You are going to need that if you're going to get this to work. Are you sure you want to use HTTPS? Your other URL is not HTTPS. Then you will need to set up your virtual host for *.scheduler.com to work properly with that certificate. Only having:
<VirtualHost *:443>
ServerAlias *.scheduler.com
DocumentRoot "/var/www/html/progetti/scheduler"
</VirtualHost>
Is not going to be anything like enough. You need all the mod_ssl stuff setting up in there as you have with the other virtual host. You could just use that default HTTPS host instead of adding another one, and modify it.
The first thing to do is get your hosting working for https://*.scheduler.com/ and then just point it at the right place.
What do you mean your endpoint is http://webserver/scheduler? This is not a valid domain name. Please clarify what you mean by that and I will update my answer with more information. Is the code on the same server?
--
Update
So to do this without SSL, add the following to your "000-default.conf", after what is currently there:
<VirtualHost *:80>
ServerAdmin localhost#gmail.com
ServerName www.scheduler.com
ServerAlias *.scheduler.com
UseCanonicalName off
DocumentRoot /var/www/html/progetti/scheduler
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html/progetti/scheduler>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
--
Update
To make http://webserver/foo.scheduler.com work and serve /scheduler, add this to the VirtualHost that was already there. Not the new one added above, the original one at the top.
RewriteEngine on
RewriteRule ^(/[^./]+\.scheduler\.com)(?:$|/(.*)$) /scheduler/$2
Let me know any problems. If you would prefer to put it in your .htaccess it will need updating.
Note: I'm taking literally your statements that the app is using http:// and the clients will use https:// URLs. Also I'm assuming that the clients are hitting the same server as the one that hosts the app.
Probably the simplest way to do this is to configure one VirtualHost for your actual application and a separate one for the other URLs.
So assuming your application lives in /var/www/html/scheduler, then your existing VirtualHost looks like:
<VirtualHost *:80>
ServerName webserver
DocumentRoot "/var/www/html"
</VirtualHost>
You would need to add change your conf.d/ssl.conf to have something like:
NameVirtualHost *:443
<VirtualHost *:443>
ServerAlias *.scheduler.com
DocumentRoot "/var/www/html/scheduler"
</VirtualHost>

Rewrite spare domains to main domains with .htaccess

We have a site with an English and Spanish version, each on a different domain. We also have a few spare domains for each language which we'd like to redirect to the language's main domain.
Specifically:
estadiosfutbol.net/..., estadiosfutbol.org/... and estadiosfutbol.info/... should all redirect to https://estadiosfutbol.com/...
worldfootballstadiums.com/..., worldfootballstadiums.info/..., worldfootballstadiums.org/... and worldfootballstadiums.net/... should all redirect to https://worldstadiums.football/...
I'm struggling with the rewrite rules so any help would be greatly appreciated.
There are two ways this can be done. The first is the simpliest, but is not always practical.
First Method
This method does not require HTACCESS files. In your Apache server configuration you just need to add ServerAliases for each of the domains that you want it to handle. (You must make sure all the domains are pointing at the same machine)
The Code
NameVirtualHost *:443
<VirtualHost *:443>
ServerName estadiosfutbol.com
ServerAlias estadiosfutbol.info estadiosfutbol.net estadiosfutbol.org
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:443>
ServerName worldstadiums.football
ServerAlias worldfootballstadiums.com worldfootballstadiums.net worldfootballstadiums.info worldfootballstadiums.org
DocumentRoot /www/otherdomain
</VirtualHost>
Note: This will only redirect if the user tries to access the website using SSL. (eg ) If you want it to redirect all traffic from both port 80 and port 443 you would need to make separate virtual hosts and use the second method to achieve the redirection.
Second Method
The second way is a little more complicated, but works in almost all situations. There a two main steps that need to be carried out in order for this to work properly:
Make sure that whatever server software you are using is setup to be looking for all the domains. The server has to have a VirtualHost(Apache) that is listening for each domain in order for the next step to do anything.
Create a .HTACCESS file under each domains' root that looks similar to this:
The Code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !estadiosfutbol.net$ [NC]
RewriteRule ^(.*)$ https://estadiosfutbol.com/$1 [L,R=301]
Note: You will need to change the third line on each domain to be the domain to rewrite from (eg estadiosfutbol.net/, estadiosfutbol.org/ and estadiosfutbol.info)
Note: Changing the forth line is all that is required for the separate domain.

apache - redirect subdomain to another domain

I need to redirect every subdomain into the domain it belongs changing the subdomain name into a parameter using mod_rewrite, and I'm not sure how. Also, I need to "reindex" the parameters so that the subdomain name becames the first parameter of the uri and the other parameters of the uri follow it by their own order. Something like this
category.domain.com/search/flowers
to
domain.com/category/search/flowers
Any thoughts on how to achieve this using mod_rewrite?
Cheers!
You can do this with one VirtualHost for all of the subdomains:
<VirtualHost *:80>
ServerName category.domain.com
ServerAlias foo.domain.com bar.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*).domain.com
RewriteRule (.*) http://domain.com/%1$1 [R=301,QSA,L]
</VirtualHost>
For it to work properly, you must have something set as the ServerName, so just choose one and list the rest of your subdomains on the ServerAlias line.
You can have multiple ServerAlias lines, so you could break them into multiple lines for readability if you have a large number of subdomains.
In the RewriteRule, the %1 matches the first matched pattern in preceding RewriteCond lines.

Redirect/rewrite alternative domains to root domain with apache2/htaccess

I 2 domains with and without the www prefix. When a user visits any of these domains, I want it to automatically reroute to a chosen 1 of them.
For example:
domain.com
www.domain.com
domain.co.uk
www.domain.co.uk
When a user visits www.domain.com, domain.co.uk or www.domain.co.uk, it will rewrite to domain.com
So far, I have my apache2 virtual host block setup like this:
<VirtualHost *:80>
ProxyPass / http://localhost:3060/
ProxyPassReverse / http://localhost:3060/
ServerName domain.com
ServerAlias www.domain.com
ServerAlias domain.co.uk
ServerAlias www.domain.co.uk
</VirtualHost>
But this doesn't do the rewriting/rerouting. I need to also make sure that it takes into account any paths. For example, www.domain.co.uk/test would change to domain.com/test
Any ideas how I can do this in the virtual host block? I'm assuming I would split the 3 domains to be rewritten into a separate block and treat them there, but really not sure how to accomplish all the rules I need.
Per your comment, you want to redirect the three aliases to the main domain, and you've stated that you would like to do this within the virtual host configuration.
<VirtualHost *:80>
...
RewriteEngine on
# If using one of the aliases ...
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk
# ... redirect to the main domain
RewriteRule ^(.*)$ http://domain.com/$1 [R=302,L]
</VirtualHost>
You can also add the Rewrite* directives in your domain's .htaccess file.
To make the redirect permanent, change 302 to 301 - this basically instructs browsers and search engines to cache the redirect.