Rewrite spare domains to main domains with .htaccess - apache

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.

Related

Apache VirtualHosts multiple ServerAliases with different TLDs

I am working with several domains which all follow a similar pattern of redirects. Instead of writing out each domain as it's own virtual host, I am attempting to make the file more maintainable using only one:
<VirtualHost *:80 *:443>
ServerName domain.xz
ServerAlias *.domain.xx *.domain.xy
RewriteEngine on
RewriteRule ^/(.*)$ https://xz.newdomain.com/$1 [QSA,NC,L,R=301]
</VirtualHost>
In the above example domain.xx will redirect to xz.newdomain.com correctly however domain.xy will not. I have checked the documentation and cannot determine the reason that this does not work.
Is it possible to make this work as I intend or will I have to make them separate VirtualHost configurations?
You should add also second level domains names in ServerAlias, i.e:
ServerAlias domain.xx *.domain.xx domain.xy *.domain.xy

Redirect but keep the domain the same

I have an owncloud server, and I would like to setup a second short domain, to keep the shared links short.
lets say we've the longdomain.com and short.com
Heres is my httpd lines
<VirtualHost *:80>
ServerAdmin email#adderss.com
DocumentRoot /var/www/dir/public_html
ServerName short.com
ServerAlias www.short.com
RewriteEngine on
RewriteRule ^/([A-Za-z0-9]{4,12})$ https://www.long.domain.com/public.php?service=shorty_relay&id=$1 [QSA,L]
ErrorLog /var/www/dir/error.log
</VirtualHost>
With the current lines, short.com redirects to exactly where I need, but I would like this redirect to be on the background and keep the short domain on the user's browser.
How can I do this?
Update:
with this in my short domain virtual host I can visit my owncloud using the short domain.
For example: short.com/index.php/apps/files/
the long domain is vanished. I think Im one step forward now.
ProxyPass / https://www.long.domain.com
ProxyPassReverse / https://www.long.domain.com
The next step is to use the the regex so I can load only shorty id links.
How can I combine the rewrite regex above with the proxypass.
I've tried ProxyPassMatch butI havent figured out how to use it properly
Any ideas?
If you can't make the same content addressable by short.com, short.com can proxy to the long domain by loading mod_proxy, mod_proxy_http, and changing your rewrite flag from R to P.

Can I configure Apache on my laptop to forward to real site for all requests except one?

I want to run some local tests on a site I have. The site is accessible at www.mysite.com. I want one particular file to be fetched from my local machine. I thought I could maybe achieve this by
installing Apache locally
adding 'localhost www.mysite.com' to my hosts file
configure Apache to forward all requests to www.mysite.com except for requests for the particular file www.mysite.com/myapp/myfile.css, which should be served from the Apache web server running locally.
Firstly I am not sure whether that set-up would work - in the case where a file is requested that is not my special case, the request would be forwarded to www.mysite.com/... , but would that then (because of the entry in my hosts file) go back to my local Apache server and into some infinite loop?
Secondly (and only relevant if the above is not true), how would I configure Apache to do that? I guess I need a ProxyPass but I'm having trouble figuring out exactly what.
Thanks for any help.
Paul
I don't think you'll be able to do this the way you're suggesting as you'll never be able to perform a lookup to proxy to www.mysite.com if you've defined it as localhost.
You could create another domain in your hosts file, say local.mysite.com and host the desired website files there and proxy everything else to www.mysite.com:
<VirtualHost *:80>
ServerName local.mysite.com
DocumentRoot ...
<Directory ...>
...
</Directory>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/myapp/myfile.css
RewriteRule ^(.*)$ http://www.mysite.com/$1 [P]
</VirtualHost>
Or if www.mysite.com works directly using the IP (i.e. not via virtual hosting) you could point localhost to mysite.com and use the real IP in the rewrite proxy.
<VirtualHost *:80>
ServerName www.mysite.com
DocumentRoot ...
<Directory ...>
...
</Directory>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/myapp/myfile.css
RewriteRule ^(.*)$ http://1.2.3.4/$1 [P]
</VirtualHost>

Redirecting New Domain Name to Server

I recently purchased a new domain name from 1and1.com and used their HTTP redirect option to point to the address of my server. Let's say, for example, the fresh domain is new.com and the established server is old.com.
I have it redirecting to old.com/new via 1and1's configuration page, which works, save for the fact that when I visit new.com, it changes the browser's URL to old.com/new. This is obviously not what I want to happen.
I've set up htaccess rules:
# BEGIN New.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^new.com
RewriteRule ^(.*) http://old.com/new [P]
</IfModule>
# END New.com
Likewise, I've done the Apache configuration of Virtual Hosts:
<VirtualHost *:80>
ServerName www.new.com
DocumentRoot /www/old/html/new/
</VirtualHost>
I then proceeded to flush my local DNS cache. Yet still, it persists in changing the address bar in my browser to old.com/new. What am I missing? Does it just need time to propagate or have I misconfigured / failed to properly set something up?
You need to change the 1and1's "new.com" DNS entry to point to the same IP that "old.com" is using. While the htaccess rule (which I assume is at the new.com document root) kind of does what you want, it requires the mod_proxy be loaded, which is something I doubt 1and1 hosting allows.
What you need to do is set it up such that when you go to a site like this and do a DNS lookup for new.com, you get the same IP as when you lookup "old.com".
On old.com's server, you have the vhost setup:
<VirtualHost *:80>
ServerName www.new.com
DocumentRoot /www/old/html/new/
</VirtualHost>
which should be all you need to at least access the contents in /www/old/html/new/.

redirecting www.subdomain.example.com to subdomain.example.com

I've had some users trying to access a site that is registered as subdomain.example.com with www.subdomain.example.com.
is there some sort of .htaccess rule I can add to redirect people that arrive using www.subdomain.example.com to subdomain.example.com?
Also, do I have to change DNS stuff?
Sure, use a directive like:
<VirtualHost *:80>
ServerName www.subdomain.example.com
Redirect permanent / http://subdomain.example.com/
</VirtualHost>
Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).
Also, yes you will need to change DNS records, because www.subdomain.example.com is a distinct hostname that needs its own A (or CNAME) record to point the browser to an appropriate server in the first place.
RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com
RewriteRule (.*) http://subdomain.domain.com/$1 [R=301,L]
You need to add a virtual host directive in httpd.conf and Redirect Permament to the correct subdomain and add the additional DNS entry (CNAME is fine)