Question

I'm trying to understand a rule inside a .htaccess file that's causing a problem.

The rule is:

<IfModule mod_rewrite.c>
    Options +SymLinksIfOwnerMatch
    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME} !.?folder1.? [NC]
    RewriteRule .? "/home/user1/public_html/sitefolder/folder1/file.php" [L]
</IfModule>

Am I correct in my understanding that this will open /folder1/file.php any time the user visits a URL that does not contain 'folder1'?

Was it helpful?

Solution

Am I correct in my understanding that this will open /folder1/file.php any time the user visits a URL that does not contain 'folder1'?

That's correct.

Some notes from the mod_rewrite documentation:

The variables SCRIPT_FILENAME and REQUEST_FILENAME contain the same value - the value of the filename field of the internal request_rec structure of the Apache server. The first name is the commonly known CGI variable name while the second is the appropriate counterpart of REQUEST_URI (which contains the value of the uri field of request_rec).

If a substitution occurred and the rewriting continues, the value of both variables will be updated accordingly.

That means the condition checks if the string folder exists anywhere in this variable.

The rule doesn't have a match in it specifically, but the target is the fully qualified absolute path of the file on the filesystem. The rule's target can be either a file-path or a URL-path, and in this case, it's a file-path. Mod_rewrite isn't always the brightest when it comes to figuring out which is which, so it's possible for it to be confused with this being a URL path, but probably not.

Once the request has been rewritten to /folder1/file.php, then the rewrite engine loops, the rule gets applied again, but this time, the %{SCRIPT_FILENAME} variable contains "folder1" and the rule isn't applied.

OTHER TIPS

If you want to trigger the RewriteRule any time a URI does not contain folder1 (exact case), you could do

RewriteEngine On
RewriteCond  %{REQUEST_URI}  !folder1 
RewriteRule ^(.*)$  /folder1/file.php

Note that it's NOT a full filepath as you had. Now, if what you're trying to do is rewrite anything not starting with /folder1/... to /folder1/... (IOW, stick /folder1/ in front of any URI that lacks it, because you moved your application down a level), it would be

RewriteEngine On
RewriteCond  %{REQUEST_URI}  !^/folder1  [NC]
RewriteRule  ^(.*)$  /folder1/$1  [L]

That also transfers over any Query String from the original URL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top