0

My goal is to redirect from domain.com/app/public/index.php?route=news/test/action to domain.com/news/test/action.

I used the following regex:

RewriteRule ^([A-Za-z0-9-/]*)$ app/public/index.php?route=$1 [NC]

This works when I call domain.com/news/test.

Now I want to also consider files to be routed so I include dots (.) in the regex like this:

RewriteRule ^([A-Za-z0-9-/.]*)$ app/public/index.php?route=$1 [NC]

Now when I call domain.com/news/test the output of $1 is not news/test but app/public/index.php.

What happened?

Dollique
  • 952
  • 2
  • 10
  • 29
  • Sorry, but you must have confused something in your test. `app/public/index.php` is _not_ part of the request string you _claim_ you used. It is impossible that it is captured. So most likely you ran into a rewriting loop. – arkascha Apr 19 '17 at 08:20
  • I never heard of a rewriting loop. Is it possible that the rule rewrites itself? – Dollique Apr 19 '17 at 08:28
  • 1
    A rule (obviously) can not "rewrite itself". But a simple google search for a "rewrite loop" would have helped you: the rewrite engine is started again after your rule matches and now the rule is applied a _second_ time to the _new_ url resulting from the first rewrite. – arkascha Apr 19 '17 at 08:43
  • 1
    Look up the `L` flag in the [mod_rewrite docs](http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l) - basically it means "after this rule has been applied, don't apply any more". – daiscog Apr 19 '17 at 09:02
  • @megaflop this is not exactly true like I found out here: http://stackoverflow.com/a/13446636/1402958 – Dollique Apr 19 '17 at 09:13

2 Answers2

0

you used /. which means / or any symbol . If you want to use dot, use \.

diavolic
  • 722
  • 1
  • 4
  • 5
  • I tried `RewriteRule ^([A-Za-z0-9-/\.]*)$ app/public/index.php?route=$1 [NC]` but get the same result. – Dollique Apr 19 '17 at 08:23
  • I'm pretty sure `.` just means `.` when inside a character class (`[.]`). It would be pretty pointless for it to retain the "any character" meaning. – IMSoP Apr 19 '17 at 09:08
0

Thanks to @arkascha I found the answer is a rewrite loop.

This code solved the problem:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-/\.]*)$ app/public/index.php?route=$1 [NC]

Thanks!


EDIT: My solution seems to have a problem with existing files like CSS files. It also redirects those. I will update as soon as I have a better way doing this...

Dollique
  • 952
  • 2
  • 10
  • 29