Mobile app version of vmapp.org
Login or Join
Murphy175

: Properly redirect a site to WWW and HTTPS 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

@Murphy175

Posted in: #301Redirect #Htaccess #Https #ModRewrite

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 (.*) www.example.com/ [R=301,L]


Also I submitted all 4 variants of my urls to google webmaster tools. I saw they have used 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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Murphy175

2 Comments

Sorted by latest first Latest Oldest Best

 

@Margaret670

Try this and see if it helps.

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

RewriteCond %{HTTPS} !on

RewriteRule ^(.*)$ www.yourdomain.com/ [R=301,L]

10% popularity Vote Up Vote Down


 

@Jamie184

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.



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. (?)



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.



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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme