0

example.com , now what I need is request comes to example.com and www.example.com redirect to https://www.example.com,

This is my apache redirect code

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

If I add RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]

browser says too many redirects or redirect loop, how to solve this, I have sub domain configuration also so the condition not to affect subdomain redirects...

1

You don't need mod_rewrite for this

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    Redirect "/" "https://www.example.com/"
</VirtualHost >

But if you really want to use mod_rewrite

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://www.example.com/%{REQUEST_URI} [R=301,L]
</VirtualHost >
  • see, the thing is i have some sub domain also , so I cant make Redirect "/" "example.com" , I need www.example.com and exmaple.com to example.com – Navin Feb 4 '16 at 6:48
  • Just change the redirect targets and/or add the subdomain the ServerAlias. Or create a new vhost for the subdomain and redirect it appropriately. – Iain Feb 4 '16 at 6:49
  • How to do that one? – Navin Feb 4 '16 at 6:52
  • 1
    A friendly search engine (even the one in the top right) and the documentation are your friend, I've stopped doing Reading Documentation As A Service. – Iain Feb 4 '16 at 6:56

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.