I am trying to redirect users to an a different url when they access my webapp from a particular url. For example, if they use to visit the web app, I want to redirect them to

This is my .htaccess file:

RewriteEngine On
    RewriteCond %{HTTP_HOST} !$ [NC]
    RewriteRule ^(.*)$$1 [L,R=301]

When I access the site using it redirect to but it repeatedly concatenates it in the URL bar. For example: etc.

I also get the error ERR_TOO_MANY_REDIRECTS.

  • Whilst this would create a redirect-loop, by itself this won't result in the redirect-loop as stated, unless there is an additional conflict with other directives (or you were seeing a cached response)? – MrWhite Aug 15 '19 at 12:05
  • 1) Are you sure your users access your website with an URL containing an IP address? While technically ok, this is not the usual case. 2) Since HTTP_HOST is, by its name, the host header in the HTTP request, which is only the hostname part of the URL and not the full URL, your RewriteCond will always be true as HTTP_HOST will never be the string – Patrick Mevzek Aug 15 '19 at 15:32

The following work for me:

RewriteCond %{HTTP_HOST} ^$
RewriteRule ^/?$ [R=301,L]


As suggested below by MrWhite, the above redirect only redirects the root. The below redirect will redirect all sub directories.

RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$$1 [L,R=301]
  • 1
    Although this only redirects the document root - is that the intention? Strictly speaking, you should also append a trailing slash, after the hostname+port - otherwise you are reliant on the user-agent appending the slash on the redirected request. – MrWhite Aug 15 '19 at 12:02
  • @MrWhite I did not consider that, I have added a trailing slash to the end of both address but only the document root is being redirected. Any advice? – Chris Aug 18 '19 at 0:09
  • 1
    You would need the RewriteRule directive you had initially (in your question) - but with the slash. ie. RewriteRule ^(.*)$$1 [L,R=301] – MrWhite Aug 18 '19 at 10:33

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.