0

WHAT I HAVE

Thanks to this discussion I've solved the problem of the "always https" redirect using this in my .htaccess:

# Redirect to httpS by default
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Redirect to www if third level domain is not set
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

WHAT I WANT

As per the current configuration, https://example.com is correctly redirected to https://www.example.com.

BUT I need that if the URL contains wp-admin it isn't redirected to www.

So, for example, https://example.com/wp-admin hasn't to be redirected. And also every subpage of the path wp-admin hasn't to be redirected.

https://example.com/wp-admin/login.php has to be visible without redirecting to www but from the second level domain.

CONTEXT (if you are curious about why I need this configuration)

I have the domain example.com.

This domain has some third level domains and has a Wordpress admin area at example.com (second level):

  • example.com
  • www.example.com
  • help.example.com
  • another.example.com

But

  • www.example.com is a Symfony app that runs on Heroku;
  • example.com is a Wordpress multisite installation that runs on DigitalOcean;
  • help.example.com and another.example.com are Wordpress sites handled with the multisite of example.com

For these reasons I need to redirect the second level domain to www if in the path there isn't wp-admin. In all other cases I need the redirect to www.

Aerendir
  • 6,152
  • 9
  • 55
  • 108
  • you want to force them into https with www then ecxlude example.com/wp-admin only from being with www but still https , right? what about example.com/wp-admin without www ? do you want also to change it without www? – Mohammed Elhag Mar 14 '18 at 14:18
  • I always want to force the use of `https`. Then I want to access `https://example.com/wp-admin` but want to redirect root `https://example.com` to `https://www.example.com` because it is the main app. I've updated the question hoping it is now more clear. – Aerendir Mar 14 '18 at 17:04

2 Answers2

1

You want redirect "http to https" ? put this in htaccess

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
php-Lover
  • 13
  • 5
0

Ok, it was as simple as adding a second RewriteCond.

The complete .htaccess is this:

# Aerendir - Force SSL
RewriteCond %{HTTPS} off

# First rewrite to HTTPS:
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Then redirect to www
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{REQUEST_URI} !wp-admin [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Aerendir
  • 6,152
  • 9
  • 55
  • 108