5

I recently found out a problem in .htaccess file in my website. When I started the site I did a 301 redirect to redirect all non-www and http request to www with https. I added this lines of code and it worked very well.

RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

Also I submitted all 4 variants of my urls to google webmaster tools. I saw they have used https://www in search engine results as well. Then later when I tried to edit the .htaccess code there were no any rule I previously added. But the redirect still worked as it should.

Is this normal? I didn't use <IfModule mod_rewrite.c> tags when adding the code. Is it okay or should I add those opening and closing tags and add it again?

2

Then later when I tried to edit the .htaccess code there were no any rule I previously added. But the redirect still worked as it should.

301 redirects are cached hard by the browser, so if the redirects are no longer in the .htaccess file (or moved to the server config) then it's quite possible you are seeing a cached response. Clear your browser cache and retest.

Do you have HTTP Strict Transport Security (HSTS) enabled on your domain? If yes, then after the initial HTTPS request the browser will always (or rather, for a specified period of time) request the HTTPS version automatically. This will also not require the redirect directive (after the initial request), but new users will not be redirected, until they have first visited the HTTPS version.

It's also possible that your server-side code (CMS / WordPress?) is performing this canonicalisation and redirecting to your canonical URL. At the end of the day you need a "redirect" somewhere in your code for the site to redirect, this doesn't happen by magic.

  1. Is this normal?

Directives "disappearing" from .htaccess is not normal. Although it's possible that updates to your CMS (eg. WordPress??) could have overwritten these. (?)

  1. I didn't use <IfModule mod_rewrite.c> tags when adding the code. Is it okay or should I add those opening and closing tags and add it again?

You don't need the <IfModule> wrapper. In fact, it is probably better to not have it. The <IfModule mod_rewrite.c> wrapper is only required if your site is intended to function "normally" without mod_rewrite or you have other modules that are dependent on mod_rewrite.

Note that if you are using WordPress... WordPress uses the <IfModule> wrapper because it is designed for maximum compatibility and can function without mod_rewrite being installed. But if mod_rewite is not available then you lose some URL formatting capability.

  1. If you have a better way to do the redirect...

This redirect looks good. The only reason to change it would be if it's not working. (eg. You started serving HTTP from a different port or something?) The only improvement would be to use your server config instead and disable .htaccess.

  • I cleared the cache and it's still working. I checked with an old backup which was taken a month ago and it was not there as well. So I think it works fine. Any comment? – Anjana Nilupul Mar 10 '17 at 14:39
  • It's possible that WordPress is handling the canonicalisation. (I assume you are using WP?) Although this would be less efficient than letting Apache handle it. I'm also assuming you are on a shared server and don't have access to the server config? – MrWhite Mar 10 '17 at 16:23
  • Yes, I'm on WP. I have seen that misconfiguration redirection hurts SEO. Also I'm on a shared server. What should I do next? Should I add above code again in the htaccess file? – Anjana Nilupul Mar 11 '17 at 7:42
  • What makes you think the redirection is misconfigured? Are you not always redirected to https://www.example.com/...? Exactly how (or where) the redirect is implemented makes no difference to SEO, providing it is a 301 permanent redirect. However, you should know your system, check the network traffic to make sure you are seeing a single 301 redirect. If WP overwrote the directives in .htaccess during an update then it's probably that you put the directives in the wrong place - if inside the # BEGIN WordPress section then they will probably get overwritten. – MrWhite Mar 11 '17 at 11:43
0

Try this and see if it helps.

RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]

RewriteCond %{HTTPS} !on

RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
  • 1
    This isn't answering the question - the .htaccess is working, the question was whether it is normal the code was missing. Also, "try this and see if it helps"? the .htaccess file is not something to experiment with :P – Steve Mar 17 '17 at 5: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.