开发者

mod_rewrite - redirecting to several different PHP files

开发者 https://www.devze.com 2023-01-20 15:20 出处:网络
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

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>


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


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...


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号