Forum Moderators: phranque

Message Too Old, No Replies

not able to target some files with an apache command in .htaccess

         

jboy

2:30 pm on Feb 21, 2011 (gmt 0)

10+ Year Member



Hello,

How do I find out what's going on here? I've reduced this to a very simple situation to demonstrate the oddity.

I have this real, actual (it appears listed in my FTP app) file here:

domain.com/c/test.css.php

In this file:

domain.com/.htaccess

I have:

CookieTracking off

Options -Indexes -MultiViews

Header set MyHeader "Hello"

RewriteEngine On
RewriteRule ^([^.]+)\.css$ /$1.css.php [NC,L]


This works fine when I access
http://www.domain.com/c/test.css
in a browser. That is, the domain.com/c/test.css.php file is accessed fine, and, it has the MyHeader set for it.

(Note: I'm just setting a header here for test purposes, because it seems like a very simple apache command -- so setting a header isn't my actual end goal. That apache line will be SetOutputFilter DEFLATE in the proper version but I'm just trying to work out why the various targetting methods aren't working at the moment)

But I want to restrict the MyHeader to either just the domain.com/c/ directory or to just files which end with .css.php

First I try moving
Header set MyHeader "Hello"
from the root .htaccess to a new .htaccess file here:
domain.com/c/.htaccess

Doesn't work; No header on access to
http://www.domain.com/c/test.css
(nor on access to
http://www.domain.com/c/test.css.php
for that matter).

Why might that not be working?

Second I try a different way. I go back to the original set up (the header line in the .htaccess in the root directory and deleting the domain.com/c/.htaccess file) but changing the .htaccess to this:

CookieTracking off

Options -Indexes -MultiViews

<FilesMatch "\.css\.php$">
Header set MyHeader "Hello"
</FilesMatch>

RewriteEngine On
RewriteRule ^([^.]+)\.css$ /$1.css.php [NC,L]


Doesn't work. I try (I'll just repeat the FilesMatch part from now on):

<FilesMatch "\.css(\.php)?$">
Header set MyHeader "Hello"
</FilesMatch>


Doesn't work. I try:

<FilesMatch "\.php$">
Header set MyHeader "Hello"
</FilesMatch>


Doesn't work. I try:

<FilesMatch "\.css$">
Header set MyHeader "Hello"
</FilesMatch>


Doesn't work. I try:

<FilesMatch "\.php">
Header set MyHeader "Hello"
</FilesMatch>


Doesn't work. I try:

<FilesMatch "\.css">
Header set MyHeader "Hello"
</FilesMatch>


Doesn't work. I try:

<FilesMatch "php$">
Header set MyHeader "Hello"
</FilesMatch>


Doesn't work. I try:

<FilesMatch "php">
Header set MyHeader "Hello"
</FilesMatch>


It works. That is I get the header on requests to
http://www.domain.com/c/test.css


It doesn't seem a very satisfactory way to make it work. The regex pattern of just "php" seems far too broad. Why might none of the previous attempts work?

Thanks.

jboy

10:17 am on Feb 25, 2011 (gmt 0)

10+ Year Member



I've found out why the FilesMatch is behaving the way it is. It's because the system I'm using uses fastcgi apparently, therefore the file name FilesMatch is looking for and matching against is /cgi-bin/php5.fcgi, which explains why only the pattern "php" works. I've tested this by making the pattern "php5\.fcgi$" and this confirms that this is the case. So it seems while using fastcgi, FilesMatch is out of action, apart from detecting PHP vs non-PHP files. Not very good. My hosting company tells me "Running the site in module mode would probably not have this problem.". Not sure if that's a good idea or not.

Why the first attempted method, having the Header command in domain.com/c/.htaccess, doesn't work, why that .htaccess file doesn't kick into action, I still don't know.

jboy

2:45 pm on Feb 25, 2011 (gmt 0)

10+ Year Member



> Why the first attempted method, having the Header command in domain.com/c/.htaccess, doesn't work, why that .htaccess file doesn't kick into action, I still don't know.

Probably the same reason apparently.

Thanks. It's been a good converstation :)

jdMorgan

4:30 pm on Mar 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Answer part 1: If you move the code to /c/.htaccess, and the .css.php file is also located in the /c/ path, then the susbtitution path of the rewriterule would need to be "/c/$1.css.php"

Answer part 2: Remember that mod_rewrite is looking at client-requested URL-paths. This is occurring *while* the server is converting the requested URL-path to an internal filepath, as a part of that process. However, the contents of the <Files> and <FilesMatch> containers are applied only after the URL-to-filepath translation is complete, and so refer to filepaths only.

The distinction between URLs used "out there on the Web" and filepaths used "here, inside this server" is critical to understanding how (and when) various server functions are applied, as well as to making any sense of the difference between the internal URL-to-filepath rewrites and external URL-to-URL redirects that can be coded using mod_rewrite.

Jim