Apache: Redirect WWW to naked domain - apache

I have an application that was written using Laravel, and it generates dynamic subdomains. Thus, I have a wildcard(*) routing mechanism. Due to several complications I'm having with SSL, I need to redirect the www.example.com routes to the naked domain (example.com), preferrably in the Apache .conf files using RewriteRules.
I've done a few searches and seen a few examples, but most of what I've seen either redirects all subdomains to the naked domain (which breaks the subdomain functionality I stated above) or does the reverse of what I'm trying to do (redirects the naked domain to www) which causes a redirect loop in many of my existing routes.
Is this something that can be done using Apache?
EDIT:
I think I fixed it. For anyone else having similar problems, I used the following rules in /etc/apache2/sites-enabled/000-default.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
This redirects any URL that starts with www to its naked equivalent, which has the added benefit of stripping www from subdomains (so, for example, www.test.example.com is redirected to test.example.com which is great (although unnecessary) since www.{subdomain}.example.com is not a defined route in my app and won't work anyways.

I think I fixed it. For anyone else having similar problems, I used the following rules in /etc/apache2/sites-enabled/000-default.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This redirects any URL that starts with www to its naked equivalent, which has the added benefit of stripping www from subdomains (so, for example, www.test.example.com is redirected to test.example.com which is great (although unnecessary) since www.{subdomain}.example.com is not a defined route in my app and won't work anyways.

Related

How do I redirect all traffic to a new domain except for the home page

So I'm trying to redirect traffic that goes to other pages using htaccess from a domain to a new subdomain but I'd like to keep traffic to the homepage there.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http\:\/\/newsubdomain\.domain\.com\/" [R=301,L]
This just redirects everything to the new subdomain. That's where I'd like some help, how can I keep the traffic that comes to the homepage on olddmain.com and redirect queries to olddomain/post and have them go to newsubdomain.domain.com/post
Thanks
This just redirects everything to the new subdomain.
No it doesn't. (If everything is being redirected then either something else is doing that or you are seeing a cached redirect.)
The directives as posted actually do the exact opposite of what you're trying to do. These directives only redirect the homepage (ie. requests for the document root).
To redirect everything, except the homepage, you can do something like the following at the top of the .htaccess file:
# Redirect everything except the homepage
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule !^$ https://newsubdomain.domain.com%{REQUEST_URI} [R=301,L]
The RewriteRule pattern !^$ matches everything except the root (an empty URL-path).
You will need to clear your browser cache before testing.
Test first with 302 (temporary) redirects to avoid potential caching issues.
Additional notes:
There is no need to backslash-escape colons, slashes and dots in the RewriteRule substitution string (2nd argument) - this is an "ordinary" string, not a regex. (This unnecessary escaping is quite typical of an attempt at using cPanel's "redirect" generation.)
I assume this should be HTTPS, not HTTP?
The two conditions (RewriteCond directives) can be combined into one.

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.

Dynamic subdomain htaccess redirection to dynamic domain

I need to create htaccess rule to redirect all www. subdomains to non-www dynamically, without to enter the domain name. Htaccess is used on parking script where are parked hundreds of domains. So, for example www.site.com must redirect automatically to site.com, www.EveryNextSite.com must redirect automatically to EveryNextSite.com.
I have try many htaccess rules without success, especially for UK domains like co.uk and others, return not expected results.
After some attempts and mistakes, I done this with simple regex:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Works also with co.uk, co.za and similar domain names.

Subdomains that don't exist are indexed by Google - Zombie subdomain removal via .htaccess

I'm experiencing an unusual issue where Google is indexing subdomains of my site that don't exist. It looks like these 'subdomains' are created from misspellings such as w. ww.. There's even some instances of http://abc.www.example.com being indexed.
My main concern here is with content duplication in the SERPS as there are several of these non existent / zombie subdomains indexed by Google. I want these subdomains to either drop out of the SERPS or be redirected to the secure www. version of the website.
I'm experienced in web development, but server configuration and .htaccess / mod Rewrite rules are not my biggest strength.
There's currently a rewrite rule to force https across the site - could this be amended to force the zombie subdomains to redirect to https://www.? If it is possible, can I ensure it doesn't effect real subdomains of the site?
Current .htaccess redirect :
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,R=301,L]
To address the comments... You can't block hostnames (subdomains) in robots.txt if all subdomains are pointing to the same place (the same site), unless you internally rewrite those requests to a different robots-block.txt file. However, robots.txt doesn't necessarily prevent indexing, it prevents crawling, and if these subdomains are already indexed, then they are going to remain indexed for while if you simply block crawling.
Are you on a shared server? Or do you have your own? This will only be a "default host" issue if this is your own server and your site is the first (or only) host defined on it. (This is rare to be a shared server issue.) Otherwise, this sounds like you have a "wildcard" subdomain defined in DNS (and corresponding ServerAlias in the server config).
Remove the wildcard subdomain and it will resolve your issue.
Otherwise you can redirect (or block) any non-canonical hosts in .htaccess. For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteRule ^ - [F]
The above would send a 403 Forbidden for any request that is not for example.com or www.example.com.
Aside:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,R=301,L]
This is not correct. It only canonicalises http://example.com. It won't canonicalise/redirect http://www.example.com or https://example.com since the two conditions are implicitly AND'd. You need to OR the two conditions:
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{SERVER_PORT} 80
In order to match example.com or http.

Is there a way to force traffic comin from www.abc.com to go to abc.com via htaccess or dns?

I am having some trouble with a flash file that has hardcoded urls in it. The flash loads fine if I visit from abc.com. But if I try to view it from www.abc.com it wont load.
So my question is, how do I got about forcing all traffic, including that coming from www.abc.com to be directed to abc.com instead.
This is what I use, in conjunction with apache's mod_rewrite module. It's domain-agnostic.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Yes. Look into mod_alias if you're running Apache, which I'll assume because you talk about .htaccess. A directive similar to
RedirectMatch http://www.abc.com(.*)$ http://abc.com$1
might do some good.