0

I have a problem with my wordpress fresh installation. I'm using the .htaccess to redirect all my traffic on https, but every time I see a 302 error page. How I can fix this?

here is the .htaccess file code

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

Options All -Indexes
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
  • Ensure your site's home URL is https - this can be found in the wp_options table. If your site is multisite, check the site's site_url as well. – phatskat Mar 1 at 19:11
  • "I see a 302 error page" - What do you mean by this? Is the browser reporting a redirect loop? – MrWhite Mar 1 at 21:43
1

First, you should not customize anything between # BEGIN WordPress and # END WordPress, it's just not a good practice. You should add your own rules above WP rules. And you should also make use of some flags, like "L".

# BEGIN Custom
<IfModule mod_rewrite.c>
Options All -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
</IfModule>

# END Custom

# 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

Also, instead of defining WP site URL through admin you should define it using constants in wp-config.php. Here is a snippet you could make use of:

$protocol = ! empty( $_SERVER['HTTPS'] ) ? 'https://' : 'http://';

define( 'WP_HOME', $protocol . 'yoursite.com' );
define( 'WP_SITEURL', WP_HOME );

For more info: https://codex.wordpress.org/Changing_The_Site_URL

  • Good advice, although I am not a big fan of setting site URLs in the wp-config.php file. I'd rather ensure that the proper values are in the wp-options table. If they are set in wp-options correctly, the define statements are not needed. From your link about using define: "This is not necessarily the best fix, it's just hardcoding the values into the site itself. You won't be able to edit them on the General settings page anymore when using this method" . – Rick Hellewell Mar 1 at 19:44
  • In my wp config for now, I've this line of code: if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)$_SERVER['HTTPS']='on'; But honestly I prefer to use the .htaccess method and then setting the address inside the wp options – ZWPDev Mar 1 at 19:51
  • In this case, it doesn't matter where you set the WP site URL. Despite that, it would be much better to have the flexibility of having it set within wp-config.php. Anyway, did it work? – filipecsweb Mar 1 at 19:54
  • Yes, thanks for the help and the clarification about the wordpress predefined .htaccess rules. I didn't know that they are sensible to any modification, this because usually I don't use wordpress for all my projects and then the .htaccess rules are always valid if writed correctly. – ZWPDev Mar 1 at 20:10
  • 1
    $_SERVER['HTTP_X_FORWARDED_PROTO'] - That would imply your site is behind a front-end proxy that is handling the SSL? If that is the case then the HTTPS server variable in .htaccess won't be set as you expect (and you will get a redirect loop). (?) – MrWhite Mar 1 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.