views:

51

answers:

5

I want any request to /files/ to be redirected to one php script, and all others (that aren't for specific file types) to be directed to another. No matter what I try I can't get this to work. This is from my apache2 virtual server config file:

<Directory /var/www/test/public>

RewriteEngine On
RewriteBase /files
RewriteRule ~files/(.+) file-handler.php?id=$1 [L,QSA]
RewriteRule !\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$ /index.php?page=%{REQUEST_URI}

</Directory>

I also tried this:

<Directory /var/www/test/public>

RewriteEngine On
RewriteBase /
RewriteRule !\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$ /index.php?page=%{REQUEST_URI}
RewriteBase /files
RewriteRule ~files/(.+) file-handler.php?id=$1 [L,QSA]

</Directory>
A: 

Have you used Virtual Hosts? < VirtualHost> section should have "RewriteOptions Inherit"

xueyumusic
That may be the answer, I am using virtual Hosts and I didn't use that. I will test.
james.bcn
No, that wasn't the problem.
james.bcn
A: 

Well, I didn't solve this, but found a work around - I just redirect to a single PHP file and then do the logic in there. Don't know why I didn't think of that before...

james.bcn
A: 

I think your problem has to do with RewriteBase and/or the ~file match. Did you try just using one base, and using that for your rules? Not totally sure what you are trying with your rules, but

RewriteEngine On
RewriteBase /
RewriteRule ^files/(.+) /file-handler.php?id=$1 [L,QSA]
RewriteRule !.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$ /index.php [L,QSA]

You can just use $_SERVER['REQUEST_URI'] in PHP directly.

The other path to try is multiple <Directory> sections (one for the subdir and one for the root).

But, as you noted, just punting into a single PHP file and redirecting there is often easier to debug.

MPD
A: 
RewriteCond ^files/(.*)\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$
RewriteRule file-handler.php?id=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Not tested, but I think it will work.

First line: match any requests for www.example.com/files/*.<file types in list above>

Second line: redirect that match to file-handler.php?id=<match from above>

Third line: match everything else

Andrew Sledge
A: 

Try these rules:

RewriteRule ^files/(.+) file-handler.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !\.(pdf|gif|jpg|png|css|swf|txt|php|ico|html|js|xml|flv)$
RewriteRule !^files/ /index.php?page=%{REQUEST_URI} [L]

The first rule matches every request that’s URI path (without the contextual path prefix, see RewriteBase directive) starts with files/. And the second rules matches every request that’s path does neither start with files/ nor ends with one of the listed file name extensions.

Gumbo