• I want the URL: http://api.relaunch.local/rest/v1/akb/
  • To rewrite to: http://api.relaunch.local/?id=1873&eID=dispatch&controller=akb

Where akb is the variable part.

I tried endless variation without success, here is my last try in .htaccess:

RewriteEngine On
RewriteBase /

RewriteRule "^rest/v1/([^/]*)/" "index.php?id=1873&eID=dispatch&controller=$1" [L]

This rule is working when I use this testing service: https://htaccess.madewithlove.be?share=eef157ef-0b95-58e2-a42d-6312ee710275

But not on my Apache. I'm on Apache 2.4 with PHP-FPM 7.1

Rewrite Log:

[Sat Oct 13 19:01:56.394388 2018] [rewrite:trace3] [pid 22532:tid 123145530462208] mod_rewrite.c(482): [client] - - [api.relaunch.local/sid#7fff0281a5c8][rid#7fff038892a0/initial] [perdir /Users/bandanh/Sites/relaunch.dev/public/] strip per-dir prefix: /Users/bandanh/Sites/relaunch.dev/public/index.php -> index.php
[Sat Oct 13 19:01:56.395768 2018] [rewrite:trace3] [pid 22532:tid 123145530462208] mod_rewrite.c(482): [client] - - [api.relaunch.local/sid#7fff0281a5c8][rid#7fff038892a0/initial] [perdir /Users/bandanh/Sites/relaunch.dev/public/] applying pattern '^rest/v1/([^/]*)/' to uri 'index.php'
[Sat Oct 13 19:01:56.395788 2018] [rewrite:trace1] [pid 22532:tid 123145530462208] mod_rewrite.c(482): [client] - - [api.relaunch.local/sid#7fff0281a5c8][rid#7fff038892a0/initial] [perdir /Users/bandanh/Sites/relaunch.dev/public/] pass through /Users/bandanh/Sites/relaunch.dev/public/index.php


    <Directory /Users/bandanh/Sites/relaunch.local/public/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        DirectoryIndex index.php index.html

    <IfModule proxy_fcgi_module>
        <FilesMatch ".+\.ph(ar|p|tml)$">
            SetHandler "proxy:unix:/Users/bandanh/Sites/.vhosts/sockets/php-fpm-71.sock|fcgi://localhost"

    LogLevel alert rewrite:trace3
  • What happens exactly - nothing at all? The directive you posted looks OK - it should "work". Do you have any other directives (perhaps a conflict)? Have you confirmed that you have the appropriate overrides in place for .htaccess and that mod_rewrite is installed? "This rule is working when I use this testing service" - Actually, that "testing service" is not producing the correct result. It shows that the entire URL is %-encoded - which is not what happens (if it did, then it is unlikely your site would work). – MrWhite Oct 13 '18 at 11:35
  • During my testing I got any kind of error 404, 500. But the one from my question is now giving me a Gateway Timeout, that would point to PHP-FPM. But why is it working with http://api.relaunch.local/?id=1873&eID=dispatch&controller=akb, should apache not convert it to the query string and path that to PHP? - I removed everything else from .htacces | mod_rewrite is there. – bandanh Oct 13 '18 at 11:58
  • A 404 implies the pattern is not matching. A 500 is a syntax error or rewrite loop (check the error log). "Gateway Timeout" - not sure / rewrite loop? "why is it working with http://api.relaunch.local/?id=1873&eID=dispatch&controller=akb" - mod_dir issues a subrequest for the DirectoryIndex (index.php). The same as if you explicitly included index.php in the request. The log entries you've posted would appear to be from later in the request when mod_dir has already issued the subrequest? Try (temporarily) changing your directive to a redirect by adding the R flag` - what happens? – MrWhite Oct 13 '18 at 14:13
  • Setting the flag to [R] I get redirected to the correct URL: http://api.relaunch.local/?id=1873&eID=dispatch&controller=akb – bandanh Oct 14 '18 at 6:04
  • Changing it to a redirect tells you that the directive itself (pattern and substitution) are OK. However, do you also have a physical file that has rest as its basename? eg. rest.php? If you do then MultiViews is the problem and you should disable this (you should disable this anyway if you are not using it). ie. Remove MultiViews from your Options directive. – MrWhite Oct 15 '18 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.