skip to Main Content

I need to:

  • add www, so user will:

    • type https://example.com/test.html
    • see in browser https://www.example.com/test.html
    • get file from example.com/test.html
  • add http -> https redirect, so user will:

    • type http://www.example.com/test.html
    • see in browser https://www.example.com/test.html
    • get file from example.com/test.html
  • add subdomain silent redirect, so user will:

    • type https://new.example.com/test.html
    • see in browser https://new.example.com/test.html
    • get file from example.com/new/test.html
  • and of course combinated, for example:

    • type http://new.example.com/test.html
    • see in browser https://new.example.com/test.html
    • get file from example.com/new/test.html

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.

2

Answers


  1. 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]
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search