Foreen
Foreen

Reputation: 399

.htaccess https, www and subdomain silent rewrite

I need to:

Is this possible via .htaccess file, eventually how?

www and https forwarding is already working fine for me, but I do not know, how to do silent forwaring with subdomains:

RewriteEngine On

# https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^new\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Thank you.

Upvotes: 0

Views: 83

Answers (2)

anubhava
anubhava

Reputation: 785276

You may try these rules in site root .htaccess:

DirectoryIndex index.php
RewriteEngine On

# add www for main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

# add https for all domains
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

# for new domain silently forward to new/ directory
RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
RewriteRule !^new/ new%{REQUEST_URI} [NC,L]

Make sure you don't have any other code except these lines in site root and you must clear browser cache completely.

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133538

With your shown attempts/samples, could you please try following. Please make sure to you your htaccess rules in following way and clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
    
RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ new/index.php?$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/new [NC]
RewriteRule ^(.*)/?$ new/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]

Upvotes: 0

Related Questions