Mobile app version of vmapp.org
Login or Join
Looi9037786

: .htaccess to WWW And/Or HTTPS 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

@Looi9037786

Posted in: #Cpanel #Htaccess #ModRewrite

On my site I want to do these redirects:


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

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

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Looi9037786

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

all of them have redirected
sub.example.com To 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 example.com/<something> to www.example.com/<something> etc?

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

RewriteEngine On

# example.com RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{HTTP_HOST} !(www.)?sub.example.com
RewriteRule (.*) www.example.com/ [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 sub.example.com) then redirect to 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 (.*) sub.example.com/ [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...

# example.com (wildcard subdomain)
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{HTTP_HOST} !(www.)?[a-z0-9-]+.example.com
RewriteRule (.*) www.example.com/ [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 (.*) %1.example.com/ [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 sub.example.com (HTTP or HTTPS).

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme