1

I have a legacy application that I want to wrap in a new symfony project. In order to separate things clearly I decided to move the entire legacy application into a folder legacy which lies outside the document root.

Since some of the legacy scripts should still be called directly, I created an Alias and wrote a RewriteRule:

Alias /legacy_public "C:\project_root\legacy\public"

RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}/../legacy/public%{REQUEST_URI}  -f
  RewriteRule ^(.*)$  /legacy_public$1 [PT,L]
[...]

This works as long as there is no path info involved. For example, calling the URL www.example.org/showLogo.php correctly checks if the file exists, rewrites the URL and executes the script.

However, the script expects some path info data in order to work correctly. When calling www.example.org/inc/showLogo.php/38 the above RewriteCond does not match, because there is no file named showLogo.php/38.

After studying the documentation it is unclear to me how I should change the condition in order discard the path_info-Part before testing -f

1 Answers1

1

You could be more restrictive in your RewriteRule pattern so to create a backreference that excludes the path-info by matching only upto and including the file extension and use the full URL-path (ie. REQUEST_URI) in the substitution instead.

For example:

RewriteCond %{DOCUMENT_ROOT}/../legacy/public$1  -f
RewriteRule ^(.+\.php)  /legacy_public%{REQUEST_URI} [PT,L]

The $1 backreference in the RewriteCond TestString then only matches upto and including .php, discarding the remainder of the URL-path.

Presumably you also have other static resources (JS, CSS and images) you also need to rewrite, so you could include these in the above regex:

RewriteRule ^(.+\.(?:php|js|css|jpg|png))  /legacy_public%{REQUEST_URI} [PT,L]

OR, use another "catch all" rule, as you did initially (that assumes no path-info).

This does assume you don't have legitimate URLs that contain what "looks like" a file extension mid-URL (unlikely).

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • 1
    Thank you very much! The backreference-Part was exactly what I was looking for. – Emanuel Oster Jun 14 '19 at 11:57
  • It would be useful if Apache provided the URL-path, less any path-info, in a handy server variable, however (like you) I've been unable to find one. You can potentially _calculate_ the URL-path (less path-info) in another _condition_, but that would only complicate the above solution and only required if your URLs were more ambiguous (eg. no file extensions). – MrWhite Jun 14 '19 at 16:33