简体   繁体   中英

htaccess redirects are not going to https

This is my first time asking anything here but I've been using this site as a reference for years. Thank you all very much for your contributions over the years. They've really been helpful! I've got a problem now that I can't find an exact answer to and it's really frustrating me. Please forgive me if there is an answer on here already for this but I couldn't make the search return anything helpful.

I've got an htaccess file that is not sending individual redirects to the https version of the URL. The rule below doesn't behave as it does on other sites for this one client.

Redirect 301 /career.html /careers/

On any other of my client sites that takes the user from https://example.com/career.html to https://example.com/careers/ in one hop. On this client's site it takes the user to http://example.com/careers/ first (regardless of if the initial request had https) which is then 301'd again to https://example.com/careers/

If the URL isn't redirected by a specific "Redirect 301" rule, the URL is rewritten properly in one shot. I need this redirect to redirect to https with one hop. It works on every other site I deal with but not this one.

This is a wordpress site and wp-config lists the https version of the URL.

Below is the bit of the htaccess that deals with canonicalizing to https://www . I had to write very specific redirect rules to canonicalize to https on this server because nothing else worked even when they work on an htaccess tester and on other live servers. There's something about this server that just doesn't behave as I expect.

# Begin completely over-complicated but necessary redirects for canonicalization. Nothing else works on this dumb server.
#
# If https is off and there is no trailing slash, rewrite to https and /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
# If https is off and there is a trailing slash, just write to https.
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} (/$|\.) 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# If https is on and there is no trailing slash, rewrite to add /
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
# If https is off, there is no trailing slash, and no www, rewrite to https www and /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
# If https is off and there is no www but there is a trailing slash, rewrite to https www
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{REQUEST_URI} (/$|\.) 
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# If https is on but no www and no trailing slash, rewrite to add www and trailing slash
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1/ [R=301,L]
#
# End of canonicalization redirects.

If I request the http version of the redirected URL the URL is first rewritten to https then it goes into a 301 for non-https, then it gets a 301 for https and then finally a 200OK with the proper https URL. All that could be avoided if the "Redirect 301" rule would kick out the https version. Any idea what's going on here?

 If https:// is not in the prefix, the HTTP link loads instead. Add the following into your .htaccess in between the <IfModule mod_rewrite.c> tag: RewriteCond %{HTTPS} !=on RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L] If there were no additional modifications done to your .htaccess, it should look like the following: # 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] # Rewrite HTTP to HTTPS RewriteCond %{HTTPS} !=on RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L] </IfModule> # END WordPress 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM