2

The %{REQUEST_FILENAME} contains a string like this:

/web/webroot/index.php

I see a lot of people recommend testing if a file exists and has a .php extension like this:

RewriteCond %{REQUEST_FILENAME}.php -f

Isn't the .php after %{REQUEST_FILENAME} redundant? The variable should already contain the .php, right?

Seems like adding .php after %{REQUEST_FILENAME} would make the TestString check add an extra .php, like this:

/web/webroot/index.php.php

I'm missing something simple, I'm sure of it, but I just don't know what it is.

The context is this:

RewriteCond %{THE_REQUEST} ^.*?\.php [NC]
RewriteRule ^ - [R=404,NE,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

1 Answer 1

2

Isn't the .php after %{REQUEST_FILENAME} redundant? The variable should already contain the .php, right?

Well, yes and no, it depends what you are trying to do.

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

This rule allows you to request extensionless URLs. eg. /index or /foo and this internally rewrites the request to /index.php and /foo.php respectively.

In this instance, REQUEST_FILENAME is a string of the form /web/webroot/index. So, it's testing that /web/webroot/index.php exists before rewriting to the same. If you were to request /index.php then the condition fails and no rewrite occurs (which is correct).

However, this rule isn't strictly correct and could cause a rewrite-loop under certain conditions. See the following related question:

9
  • 1
    I see you know your stuff. I'll stare at this for a few more hours until I figure it out. I think you're saying the variable is modified by appending the .ext, but I can't find this behavior documented anywhere except on SE forums. EDIT: Yeah, okay, I get it. Thanks!
    – Jeff
    Nov 27, 2021 at 16:20
  • 1
    @Jeff The variable itself (ie. REQUEST_FILENAME) isn't modified, just the TestString (first argument to the RewriteCond directive) - as you state in the question. It's basically testing whether the requested URL + .php exists as a physical file. Although, REQUEST_FILENAME isn't strictly the correct variable to use here (although works in most cases), as mentioned in the linked question. (REQUEST_FILENAME is the "file" that the requested URL maps to.)
    – MrWhite
    Nov 27, 2021 at 16:29
  • 1
    Well since I, of course, want to use strictly the correct variables in my code, is this the one: %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f?
    – Jeff
    Nov 27, 2021 at 16:50
  • 2
    @Jeff Yes, and change the RewriteRule substitution string accordingly. ie. RewriteRule ^ %{REQUEST_URI}.php [L]
    – MrWhite
    Nov 27, 2021 at 16:53
  • 1
    @Jeff The RewriteRule directive is processed first. Only when the RewriteRule pattern matches are the preceding conditions processed. If the RewriteRule pattern is not successful then the entire rule is skipped and processing continues...
    – MrWhite
    Nov 27, 2021 at 20:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .