-1

Posting this to SE instead of ServerFault since it has a focus on web development and includes references to Symfony.

I'm having trouble creating a .htaccess rewrite with Symfony's default .htaccess setup. My intention is as follows:

  • Internally rewrite all requests to bla/something/* to something/else.
  • something/else is not a valid file in the webroot, but rather a URI handled by the Symfony application
  • request flow thus becomes: match internal rewrite, re-process the rules with something/else (which is then rewritten - and found by the Symfony app), thus serving the something/else controller while the browser's URL still shows bla/something/x.

I've build the following basic rule which works when used for 301 redirects:

RewriteRule ^bla/something/(.+)$ /something/else

thus resulting in the full .htaccess file being:

DirectoryIndex index.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

#   RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
#   RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by Apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

#    DirectoryIndex index.php
#    RewriteCond %{ENV:REDIRECT_STATUS} ^$
#    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    RewriteRule ^bla/something/(.+)$  /something/else
    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]


    # Rewrite all other queries to the front controller.
#    RewriteRule ^ %{ENV:BASE}/index.php [L]
    FallbackResource /index.php
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /index.php/
    </IfModule>
</IfModule>

Ideally, all that I've commented out should be enabled and working. I've just tried to distill it down to the most basic file. What else have I tried:

  • Flags: [PT], [L]…

  • Replacing with 301 redirects: those work, so the rule matches

  • rule order

The problem is however, that the request is not rewritten at all internally. Browser shows /bla/something/x, which is also what the Symfony application sees for the requested path.

Robin Heller
  • 400
  • 5
  • 14
  • 2
    _“Browser shows /bla/something/x, which is also what the Symfony application sees for the requested path.”_ - that is probably because Symfony takes the requested URL from `$_SERVER['REQUEST_URI']` - and that value does not change due to internal rewriting, but still contains the originally requested URL. But what is the point of this anyway - why don’t you just configure proper routing for `bla/something/*` inside of Symfony to begin with? – CBroe Apr 02 '20 at 06:44
  • 1
    @CBroe thanks for that, that was good enough. I solved it with the [P] flag since mod_proxy was available anyway. As to why I'm not using a Symfony redirect: it's a third party app where I don't have _that_ much control over routing without jumping through some hoops. From your comment I found this answer https://stackoverflow.com/questions/18618142/apache-2-mod-rewrite-and-php-modify-serverrequest-uri-value-from-htaccess which helped me solve the problem - for now. Could you turn that comment into an answer so I can accept it? – Robin Heller Apr 02 '20 at 08:39

1 Answers1

2

Browser shows /bla/something/x, which is also what the Symfony application sees for the requested path.

That is probably because Symfony takes the requested URL from $_SERVER['REQUEST_URI'] - and that value does not change due to internal rewriting, but still contains the originally requested URL.

Ideally this should be handled via the Symfony routing configuration in the first place, but if that is not possible due to lack of access, then it’ll need some other workaround.

Using mod_proxy might work, but is probably not the most ideal solution - it means the server has to handle an extra HTTP request now. But if you don’t experience any noticable performance issues with this, then it should probably do.

CBroe
  • 91,630
  • 14
  • 92
  • 150