1

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 192.0.2.4:80 to visit the web app, I want to redirect them to 192.0.2.5:8989.

This is my .htaccess file:

RewriteEngine On
    RewriteCond %{HTTP_HOST} !http://192.0.2.4:80$ [NC]
    RewriteRule ^(.*)$ http://192.0.2.5:8989$1 [L,R=301]

When I access the site using 192.0.2.4:80 it redirect to 192.0.2.5:8989 but it repeatedly concatenates it in the URL bar. For example:

192.0.2.5:8989/192.0.2.5:8989/192.0.2.5:8989/192.0.2.5:8989 etc.

I also get the error ERR_TOO_MANY_REDIRECTS.

2
  • 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, 2019 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 http://192.0.2.4:80 Aug 15, 2019 at 15:32

1 Answer 1

1

The following work for me:

RewriteCond %{HTTP_HOST} ^192.0.2.4:80$
RewriteRule ^/?$ http://192.0.2.5:8989 [R=301,L]

EDIT

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

RewriteCond %{HTTP_HOST} ^13.210.137.143$
RewriteRule ^(.*)$ http://192.0.2.5:8989/$1 [L,R=301]
3
  • 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, 2019 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, 2019 at 0:09
  • 1
    You would need the RewriteRule directive you had initially (in your question) - but with the slash. ie. RewriteRule ^(.*)$ http://192.0.2.5:8989/$1 [L,R=301]
    – MrWhite
    Aug 18, 2019 at 10:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .