I've enabled SSL on my Wordpress website and set redirection from www to non-WWW via the following code placed in .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

However, it works good only on homepage (example.com). What I am doing wrong?

  • Shouldn't your redirect directives be at the top of the file? Why is the WP section repeated twice?
    – Tom J Nowell
    Feb 22 at 14:58
  • @TomJNowell They should - I just changed it - thank you! As for the repeated WP section, I have no idea how that happened. Just fixed it Feb 22 at 15:40

You've put the directives in the wrong place, the canoncial redirects need to go before the # BEGIN WordPress section (the front-controller), otherwise, they are not going to be processed for anything other than physical files and directories. (The "homepage" maps to a physical directory, so is redirected).

For some reason you have also repeated the WordPress front-controller section, with a slight difference. You should only have one.

# BEGIN block author scans
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (author=\d+) [NC]
RewriteRule .* - [F]
# END block author scans

This section is also in the wrong place and needs to go before the # BEGIN WordPress section. You should remove the RewriteEngine and RewriteBase directives from this block.

  • 1
    Thank you so much! Now it works great :) Here's my new updated .htaccess file: pastebin.com/gcnfv8gc - does it look good now? Feb 22 at 15:42
  • You're welcome. Yes, your new updated .htaccess file looks OK. Minor point, but there's no need to repeat the RewriteEngine directive. Just have it once inside the # BEGIN WordPress section and you don't need it anywhere else. (Only the last instance of the RewriteEngine directive actually does anything.)
    – MrWhite
    Feb 22 at 15:54
  • Thank you! I'll keep that in mind :-) Feb 22 at 16:13

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.