0

Given this url:

http://test.com/myfile/product/1

and the following RewriteRule:

RewriteRule ([^/.]+)/?(.*) app/$1.php?$2

I would expect the url to become:

http://test.com/app/myfile.php?product/1

and it does when I use an online htaccess tester. But on my local dev environment I get this:

The requested URL /app/app.php was not found on this server.

Why? This can't be right, right? I suspect it is a bug caused by my setup (docker containers and dinghy-http-proxy) but since I am new to this rewriting I am not sure.

sanderdev
  • 3
  • 3

1 Answers1

0

Try this:

RewriteRule ^([^/.]+)/?(.*) app/$1.php?$2

The problem is that the regex can match anywhere in the path string, and since the / is optional the result is unlikely to be what you want.

Also, make sure that you don't have multiple rewrite rules which apply, they will all get processed by default!

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • Thanks but this still gives me the same result. – sanderdev Oct 15 '16 at 18:26
  • @sanderdev See my edit - if you have multiple rules it may be rewriting the result of another rule. – Lucero Oct 15 '16 at 18:27
  • What I don't get is how $1 contains "app" when this is not used in the url. If it matches it should contain something from the string: "myfile/product/1" right? But now it contains something from the substitution string which I really don't understand. – sanderdev Oct 15 '16 at 18:30
  • Thank but I am only using this one rule. It still doesn't work which is why I suspect this must be some bug in my setup. – sanderdev Oct 15 '16 at 18:32
  • @sanderdev I still suspect that multiple rules get executed: a first one already adds the "app" to the URL, the second (which you're working on now) then retrieves it et voilà you think that it came from the substitution. Note that Apache will process htaccess per-directory, so double check for duplicate rules. Also, if you want to stop processing further rules, add a `[END]` flag to the rule. Maybe try logging what happens: http://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite – Lucero Oct 15 '16 at 18:36
  • Guess you are right, when I add the [END] flag it does work as expected. Working in a big project and yes there are other htaccess files which may have caused it, didn't think of this thanks alot! – sanderdev Oct 15 '16 at 18:42