htaccess www sub domain redirect - apache

My problem
I have a sub domain pointing to a different server. The server has a SSL, thus when a user types in the sub domain: https://my.maindomain.co it works great. Using htaccess I'm trying to redirect all users who type in the following web address: https://www.my.maindomain.co or the http:// equivalent to the correct version (https://my.maindomain.co).
I thought the below would work nicely but it does not:
# rule for removing www on sub domains
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.maindomain\.co)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
Anybody help me out with this?
For reference I am placing this code in the htaccess on the maindomain.co server, which is hosted on a different server to my.maindomain.co

You can use this rule for your subdomain on the server where the sub domain is hosted. It should take care of your requirements.
RewriteEngine On
RewriteCond %{HTTPS} !^on [OR]
RewriteCond %{HTTP_HOST} !^my\.example\.co$ [NC]
RewriteRule ^ https://my.example.co%{REQUEST_URI} [R=301,L]
Note you must have a valid ssl certificate for www.my.example.co for this to work if they enter www.my.example.co. Otherwise it will still handle redirecting http://my.example.co to https://my.example.co.

First of all what do you mean by 'it does not work'? It would be helpful, if you could specify what is the exact error you get when you try to load https://www.my.subdomain.co
Apart from that i can think of one more factor, if you are using apache server, u might want to add 'www.my.tutella.co' to ServerAlias under virtual host configuration of my.tutella.co

You don't actually need mod_rewrite to do this.
In the .htaccess of your subdomain (or, if you have access to the main Apache configuration, that's more efficient than .htaccess files) you can use this directive:
Redirect / https://my.example.co/
...this uses mod_alias and is much easier to understand and troubleshoot than the mod_rewrite equivalents.

Related

Issues redirecting from old HTTPS domain to new HTTPS domain via htaccess?

I have tried a zillion variations of .htaccess rewrites and cannot get this to work.
I have a previous HTTPS old-domain.com that I need to forward to new-domain.io. Both are HTTPS but only the new domain has SSL certs on the server. This makes the browser trying to load old-domain.com just spin in the browser.
I already have a DNS forward that works fine ONLY with http, not https. I am thinking that I need to use something like %{HTTP:X-Forwarded-Proto} but not exactly sure how. Nothing has worked so far.
https://old-domain.com
AND https://www.old-domain.com
both need to redirect to https://new-domain.io (along with any URI like/something/this.html)
Something like this looks like it should work, but redirects infinitely.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$
RewriteRule (.*)$ https://new-domain.io/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*)$ https://new-domain.io/$1 [R=301,L]
SOLUTION --------
The new domain .htaccess file cannot fix a HTTPS redirected link by itself.
There are two ways to correctly fix it.
Remove DNS forwarding at the old domain DNS. Then make sure there are still valid SSL certs AND put a redirect on its .htaccess file to handle the redirects with something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} (w*)domain.com$ [NC]
RewriteRule ^ http://newdomain.com%{REQUEST_URI} [L,R=301]
Leave the DNS forwarding of the old domain and add a new multi-domain SSL cert at the new domain which includes BOTH domains. This is tricky because you will have to manually authenticate the old domain because the cert won't be living at the old domain host.
I choose and implemented #1 successfully.

Own example.org and can't get www.example.org to redirect

I own http://0x3c.org/ and everything is working as it should. I am using Google Domains and have Dynamic DNS set up with them to point to the Apache server inside of my house.
My issue is that http://www.0x3c.org/ does not redirect to http://0x3c.org/, instead it says that http://www.0x3c.org/ does not exist. Additionally, I do not want http://www.0x3c.org/ to exist.
I would like for it to redirect, and don't know how to go about setting it up. Can anyone guide me as to how to do this?
Thank you.
www.0x3c.org has to exist, in order to redirect the requests. You have to create a DNS (CNAME) entry for www.0x3c.org, direct it (preferably) to the same server that serves 0x3c.org and then use Apache rewrite rules, as described here:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

.htaccess forward 'www.w.' to 'www.'

I have a weird issue where Google is indexing several of my site's pages as 'www.w.example.com' which is causing issues with my security certificate.
While I'm looking for a solution, I'd like to redirect this using .htaccess but I can't seem to get it to work.
Currently I have:
RewriteRule ^(.*)www\.w\.(.*)$/$ https://www.$1/$2 [R=301,L]
But it doesn't seem to work..
I have multiple domains for this site so ideally it needs to redirect to the correct domain e.g. https://www.example1.com or https://www.example2.com
It is a bad SEO practice to have the same content available on more than 1 URLs.
You need to decide on the best URL you would like to use and then do a 301 redirect of the others to it.
You need to use a RewriteCond for matching host name:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.w\.(.+) [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]
However you might still get SSL cert warning because cert negotiation happens before mod_rewrite rules are invoked.

Confused over setting up Sub Domain / Alias as a forwarder to location on apache server

I am a little confused over the correct method to redirect/forward a domain alias (sub domain) to a directory on my website using apache.
I want the following domains http://helpdesk.xxxx.com and https://helpdesk.xxxx.com to redirect to https://www.xxxx.com/helpdesk.
I don't have a wildcard SSL cert so I am not sure if using rewrite would be the best method, I just want the server to forward any requests to those Alias on ports 443/80 to correct patch on the server.
Thanks
You can have this rule in root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(helpdesk)\.(.+)$ [NC]
RewriteRule ^ https://www.%2/%1%{REQUEST_URI} [L,R=301,NE]

Redirecting all traffic based on Host name

An external server (I'll call it "sub.origin.edu") redirects all traffic to my webpage. I want to take all traffic from this host, and redirect it to a different site (which I'll call "http://foo.target.edu/board/").
My .htaccess file is:
RewriteEngine On
RewriteCond ${HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board/ [R=302]
This doesn't seem to be working. I've confirmed (using PHP) that the host is indeed sub.origin.edu, and the .htaccess file is in the right directory, but this rule just doesn't come into effect. Any suggestions? Thanks.
(If I remove the RewriteCond, the redirect happens, so I can confirm that everything but the rewrite condition is working.)
Use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board$1 [R=302]
You used the wrong substition character ($ instead of %)
I found this question while trying to complete a re-derict for specific hostnames.
This link was of great help to understand how RewriteCond and RewriteRule work.
http://httpd.apache.org/docs/2.4/rewrite/intro.html
If sub.origin.edu is doing a 3xx redirect, then the browser will issue a new request to your server using your.server.edu as the host name. So this rule will never match that. If this is the case, there's no easy way to tell where the request was redirected from.
If they're using a CNAME, Femi has the correct answer.