1

I setup .htaccess file for my domain and sub-domain. If I write manually https then both working fine but I want to force so that no one can open without https.

Also I want WWW with my main domain and Sub-domain without WWW. Below is my .htaccess code,

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^app.domain.com$ 
RewriteCond %{REQUEST_URI} !^/app/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /app/$1 
RewriteCond %{HTTP_HOST} ^app.domain.com$ 
RewriteRule ^(/)?$ app/index.php [L]

And Is it possible to redirect user to sub-domain if they type www.domain.com/app/

Arif
  • 1,222
  • 6
  • 29
  • 60

1 Answers1

1

You can insert these 2 rules:

RewriteEngine On

# force https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

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

# remove www for app site
RewriteCond %{HTTP_HOST} ^www\.(app\.domain\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{THE_REQUEST} \s/+app[/\s] [NC]
RewriteRule ^ / [R=301,L]

RewriteCond %{HTTP_HOST} ^app.domain.com$ 
RewriteCond %{REQUEST_URI} !^/app/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /app/$1 
RewriteCond %{HTTP_HOST} ^app.domain.com$ 
RewriteRule ^(/)?$ app/index.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • where to add these 2 rules in my code. Can you please arrange it according to my code mentioned in the question – Arif Feb 28 '14 at 13:27
  • its working fine for both but if I add `www.app.domain.com` then it dont redirect to app.domain.com. Also is it possible to redirect `www.domain.com/app/` To `app.domain.com` – Arif Feb 28 '14 at 13:35
  • thank you so much. Your code is wroking perfect but one issue still i have. It doesn't prevent to access www.domain.com/app/ . Still when I type that it run this page `https://www.domain.com/app/login.php` – Arif Feb 28 '14 at 13:58
  • Can you please check my this question [http://stackoverflow.com/questions/22837602/htaccess-url-rewrite-for-sub-domain](http://stackoverflow.com/questions/22837602/htaccess-url-rewrite-for-sub-domain) , I try alot but don't know why my rule is not working. Thanks in advance – Arif Apr 06 '14 at 10:31