0

My .htaccess file doesn't redirect my domain from HTTP to HTTPS. My pages are redirected to HTTPS but not my domain.

Do you have an idea of what can cause this problem?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^maghreb-secours\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}

php_value upload_max_filesize 200M
php_value post_max_size 200M
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
freginold
  • 239
  • 1
  • 7
sihamxxi
  • 1
  • 2

1 Answers1

0

It looks like something is redirecting requests to your apex out to the www host, and your RewriteCond is configured only to work on the apex (^maghreb-secours rather than ^www.maghreb-secours)

$ curl -s --head http://maghreb-secours.com/ | grep -e "^HTTP" -e "^Location:"
HTTP/1.1 301 Moved Permanently
Location: http://www.maghreb-secours.com/
$ curl -s --head https://maghreb-secours.com/ | grep -e "^HTTP" -e "^Location:"
HTTP/1.1 301 Moved Permanently
Location: https://www.maghreb-secours.com/
$ curl -s --head http://www.maghreb-secours.com/ | grep -e "^HTTP" -e "^Location:"
HTTP/1.1 200 OK
$ curl -s --head https://www.maghreb-secours.com/ | grep -e "^HTTP" -e "^Location:"
HTTP/1.1 200 OK

Also, I'm not sure the backslash is required to escape the dot in the hostname.

Maybe this will work (including the apex in case your config changes down the line)?

RewriteEngine on
RewriteCond %{HTTPS} != on
RewriteCond %{SERVER_NAME} =www.maghreb-secours.com [OR]
RewriteCond %{SERVER_NAME} =maghreb-secours.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
neuro
  • 385
  • 2
  • 7
  • "not sure the backslash is required" - The backslash is required to match a _literal dot_ in the OP's regex. However, you've used the `=` operator on the _CondPattern_, which forces a lexicographical string comparison (not a regex), so the backslash escape should not be used here. However, using a regex would arguably simplify the conditions - you'd only need one; instead of two (do you need any?!). You should use `HTTP_HOST` if you are specifically trying to capture the requested _host_, not `SERVER_NAME` (which depends on server config). – MrWhite Feb 28 '18 at 23:06
  • Also, consider canonicalising the non/www in the _substitution_. Otherwise, WordPress will probably do this _later_ (slower). Note that the OP's original redirect was missing the `L` flag on the `RewriteRule`, which was no doubt causing some problems, as WordPress would have been triggered regardless. – MrWhite Feb 28 '18 at 23:09
  • @neuro i'v been using the code you suggested and i't works perfectly, but now i can't access to the WordPress dashboard (wp-admin) and also other pages of the website, can you help with that please? – sihamxxi Mar 05 '18 at 12:08
  • @sihamxxi sorry, I didn't see your comment until now; if you use a MySQL client with your site's MySQL username and password to connect to your site's database, you can edit the `siteurl` and `home` values in the `wp_options` database table to use `https` rather than `http`. – neuro Apr 10 '18 at 20:14