4

On my site I want to do these redirects:

  • http[s]://example.com => https://www.example.com
  • https://www.example.com => No Redirect
  • http://sub.example.com => https://sub.example.com
  • http[s]://www.sub.example.com => https://sub.example.com

I've tried many methods but all of them have redirected http://sub.example.com to https://www.sub.example.com. Which then leads to a "DNS Not Found" Or it will redirect to suddenlink search.

  • It would be helpful to see your existing directives that are failing. – MrWhite Mar 10 '17 at 11:51
5

all of them have redirected http://sub.example.com To https://www.sub.example.com

That would seem to suggest that you have a rule that simply checks for the absence of www (which is correct for the first rule, but would conflict with the second) and then prefixes this to the requested host.

I assume you also want to redirect http://example.com/<something> to https://www.example.com/<something> etc?

Try the following at the top of your .htaccess file:

RewriteEngine On

# www.example.com
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTP_HOST} !(www\.)?sub\.example\.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

This basically states... for all requests that are HTTP (ie. not HTTPS) OR are for example.com AND is not for sub.example.com (or www.sub.example.com) then redirect to https://www.example.com/....

Then add a similar rule block for the subdomain canonicalisation:

# sub.example.com
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\.sub\.example\.com [NC]
RewriteCond %{HTTP_HOST} !(www\.)?example\.com
RewriteRule (.*) https://sub.example.com/$1 [R=301,L]

Make sure you've cleared the browser cache before testing, since your earlier (erroneous) 301 redirects will have been cached. (It can often be easier to test with 302s for this reason.)

UPDATE: can I do a wildcard on the subdomain?

A subdomain of 1 or more characters...

# www.example.com (wildcard subdomain)
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTP_HOST} !(www\.)?[a-z0-9-]+\.example\.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

# <any-subdomain>.example.com
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\.[a-z0-9-]+\.example\.com [NC]
RewriteCond %{HTTP_HOST} !(www\.)?example\.com
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-z0-9-]+)\.example\.com [NC]
RewriteRule (.*) https://%1.example.com/$1 [R=301,L]

The additional RewriteCond directive for the subdomain block is required in order to capture the subdomain so we can use this in the RewriteRule substitution (%1 backreference). The (?:www\.)? part of the CondPattern is a non-capturing group that makes the www sub-subdomain optional, since we need to be able to redirect sub.example.com (HTTP) or www.sub.example.com (HTTP or HTTPS).

The %1 backreference in the RewriteRule substitution matches the first captured group in the last matched CondPattern.

  • can I do a wildcard on the subdomain? And my old .htaccess, I deleted it. – Donovan_DMC Mar 10 '17 at 21:37
  • I've updated my answer with a "wildcard" subdomain version. – MrWhite Mar 10 '17 at 23:14
  • its taking the [www.]example.com to https, but the subdomain stays at http, i want to make the subdomain go to https aswell, and on the www.*.example.com i just want to remove the www from it [www.*.example.com to *.example.com] – Donovan_DMC Mar 11 '17 at 1:19
  • sorry i ask so much i have looked around for these things and i cant find them. – Donovan_DMC Mar 11 '17 at 1:19
  • I've updated the directives slightly and removed some unnecessary NC flags and updated the regex for the subdomain to include digits (the hyphen was also omitted from the main domain's subdomain check). However, this may not change your results. Can you confirm that the subdomain points to the same area of the file system? Do you have any other directives in your .htaccess file? Do you have any other .htaccess files in subdirectories - particularly any that might relate to these subdomain(s)? As always, you will need to clear your browser cache before testing. – MrWhite Mar 11 '17 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.