-3

Can anyone help me with url redirecting with .htaccess file?

I want to redirect my url to one and only https://mydomain.com

If anyone types "www" or "http://" or "http://www" or "https://www", then the user should always go to https://mydomain.com

Thanks! Amit

mc110
  • 2,825
  • 5
  • 20
  • 21
Amit
  • 35
  • 9
  • 3
    No-one can reach your web page with just a "www" prefix. The browser always adds the protocol (http://). In any case, this is a very basic task and there are probably dozens of examples out there. What have you tried and where are you stuck? – JJJ Jul 01 '14 at 12:50
  • Dear Juhana, Thanks for your reply. As I am a new user here, I cannot post more than 2 links. So I was unable to write full url. – Amit Jul 01 '14 at 12:53
  • I don't think you understood my comment. Add your .htaccess file to the question (only the part where you have tried the redirect) and explain what happens when you try it. – JJJ Jul 01 '14 at 12:55
  • My question is if anyone try to go to my site with www or http or http[://]www or https[://]www then the user should always go to https:// (i am unable to add full domain name here. my question refers to full domain) – Amit Jul 01 '14 at 12:55
  • possible duplicate of [htaccess redirect to https://www](http://stackoverflow.com/questions/13977851/htaccess-redirect-to-https-www) – Eugene Podskal Jul 01 '14 at 13:08
  • here is the code that I am using. It is only redirecting with www prefix. RewriteEngine on RewriteCond %{HTTP_HOST} !^mydomain\.com RewriteRule (.*) https://mydomain.com/$1 [R=301,L] – Amit Jul 01 '14 at 13:25

1 Answers1

0

You need two rules. In some cases there will be a double redirection tho.

 <IfModule mod_rewrite.c>
   RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
   RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

   RewriteCond %{SERVER_PORT} !^443
   RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 </IfModule>

Edit: now it will remove the www part.

Please note that your SSL cert does only match site.com but not www.site.com, which will cause security alerts when you access it via https://www, I'm not sure you can avoid that with htaccess rules.

The first rule will trigger and redirect https:// + www to https:// without www, but the alert might fire anyway. Just give it a try.

Amit
  • 35
  • 9
ffflabs
  • 17,166
  • 5
  • 51
  • 77
  • ok so you want to always redirect to templateartist.com without www, and always use https instead of http. I'll edit the above code accordingly – ffflabs Jul 01 '14 at 14:58
  • Thanks a lot for your help. It is actually working great for www and non www redirection to https but unfortunately https+www is not redirecting. I think I dont need that too. Anyway thank you so much for your help. – Amit Jul 01 '14 at 16:23