1

I have the .htaccess file in the root public_html directory. It contains the following:

Options -Indexes
Redirect / /dali

RewriteEngine on
RewriteRule ^api/(.*[^/])/?$ http://%{HTTP_HOST}:5000/$1 [R=301,L]

I wished this to forward all requests from http://192.168.1.144/api/something to http://192.168.1.144:5000/something. And if I try this in the sandbox - it is working:

https://i.stack.imgur.com/YothN.png

And if I try resulting URL - it should work:

enter image description here

However in reality - it does not:

enter image description here

So it looks like mod_rewrite don't handles its work as I expect.

What can I do to track this out?

3
  • Given the code as posted, you would expect /api/something to be redirected as required. However, the Redirect / /dali directive is suspect. This would ordinarily result in a redirect-loop for anything other than URLs of the form /api/<anything> - since Redirect is prefix-matching and so / matches everything. However, you've stated that / is successfully redirected to /dali and no loop. If this is the case then there would seem to be something else that prevents this? Is /dali alias'd to another part of the filesystem? Any other .htaccess files in subdirectories? – MrWhite Jun 8 '20 at 1:17
  • ...I'm wondering if whatever it is that prevents the redirect loop is also preventing the mod_rewrite redirect to function as intended? This is assuming the that / to /dali redirect is being performed by .htaccess and not something else (eg. server config, application logic, etc.)? – MrWhite Jun 8 '20 at 1:19
  • "This would ordinarily result in a redirect-loop for anything other than URLs of the form /api/<anything>" - this is assuming that port 5000 references a different part of the filesystem, otherwise every URL would potentially trigger a redirect-loop. (?) – MrWhite Jun 8 '20 at 1:35
1

I don't think that you actually want a redirect. A redirect puts an ugly port number into the URL visible to the client.

It would be more desirable to use a reverse proxy it most cases. A reverse proxy has your main server connect to your backend API port, get the content and relay it back to the client without the client ever knowing about the backend URL.

To implement that, you would enable the Apache proxy modules from the command line (Debian/Ubuntu command, you may need to look up other commands on other distributions):

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart

Then add this code to your Apache configuration files, usually in your VirtualHost that handles the site.

ProxyRequests Off
ProxyPass /api/ http://localhost:5000/
ProxyPassReverse /api/ http://localhost:5000/

Alternately, after enabling the proxy modules, you can change your rewrite rule to do a reverse proxy using the P flag rather than R=301 flag if you first solve the problems of your .htaccess file not taking effect from MrWhite's answer.

RewriteEngine on
RewriteRule ^api/(.*[^/])/?$ http://%{HTTP_HOST}:5000/$1 [P,L]
1

Redirect / /dali

Ordinarily, this directive would result in a redirect loop*1 for URLs other than /api/<something> (since the mod_rewrite directive that follows takes priority), which leads me to think you don't have .htaccess overrides enabled to begin with (ie. .htaccess is not enabled)?

(*1 Unless perhaps you have another .htaccess file in a subdirectory that somehow prevents this redirect-loop, or perhaps the /dali subdirectory is Alias'd to a different part of the filesystem so the parent .htaccess file is not reprocessed? Or something else?!)

To enable .htaccess overrides, you need to set the appropriate AllowOverride directive in your server config (for the required directory). For example:

<Directory /path/to/public_html>
AllowOverride All
</Directory>

The "sandbox" tool you link to would seem to be incorrectly handling that Redirect directive and its prefix-matching behaviour*2. Also, it only makes a single pass through the code - it doesn't simulate the redirect, so "redirect-loops" are not detected.

(*2 The Redirect directive is prefix-matching. eg. Redirect / /dali matches any URL that simply starts / - which naturally matches everything. However the "mwl htaccess tester" appears to treat this Redirect directive as an exact match and only matches / exactly, ie. the document root - this is incorrect.)

Otherwise, the RewriteRule directive actually looks OK, except that it possibly matches too much, since it will also match /api/s/o/m/e/t/h/i/n/g - is that the intention?

4
  • Redirect / /dali worked. At least if I visited http://192.168.1.144/ I was found my self on ...1.144/dali page as was expected. As you've suggested I changed AllowOverride for <Directory /var/www/> to All but Apache turned not to work (500 Error) until I returned this setting to None. Regarding the last point: if IP/api/s/o/m/e/t/h/i/n/g will redirect to IP:5000/s/o/m/e/t/h/i/n/g - it is fine. But for now I don't need anything but the first word. – Roman Matveev Jun 7 '20 at 21:03
  • "Redirect / /dali worked." - Ok, although that wouldn't seem to be possible given just the code as posted - there must be something else (see my comments above)? "I changed AllowOverride for <Directory /var/www/>" - that alone shouldn't result in a 500 error?! Check the server error log for details. Although, shouldn't you be targeting the public_html directory? – MrWhite Jun 8 '20 at 1:31
  • that was my mistake: my Apache default folder was /var/www/html, not anything like public_html as usual. – Roman Matveev Jun 8 '20 at 6:00
  • @RomanMatveev Are you saying the issue was resolved by editing/referencing the correct directory? – MrWhite Jun 23 '20 at 1:20

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.