如何使用mod_rewrite将"www.somedomain.com/folder1/demo.jpg"请求重定向到其他页面?



代码块:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} mysite.000webhostapp.com$ [NC]
RewriteCond %{HTTP_HOST} !folder1
RewriteRule ^(.jpg)$ http://mysite.00xxxtapp.com/folder1/$1 [R=301,L]

我想你也想在你的文件名出现在新的网址中?(不仅仅是".jpg"(

RewriteRule ([^/]+.jpg)$ http://mysite.00xxxtapp.com/folder1/$1 [R=301,L]

RewriteRule ([^/]+.jpg)$ /folder1/$1 [L]

如果您想继续看到旧网址

编辑

所以你需要超过 1 个重写规则。

我建议的步骤

  1. 所有"假"URL重定向到某些PHP脚本
  2. /folder1/中的所有图像必须重定向或使其无法访问(建议:将它们放在文档根目录之外|(


RewriteEngine On # all images in somefolder will be parsed through a php script RewriteRule somefolder/([^/]+.jpg)$ /imageshower.php [L] # redirect to a no access image

RewriteRule folder/*.jpg /no-access.jpg [R]

php脚本将能够进行一些检查,例如"用户是否登录?"或者他是否拥有权限。

<?php
$filename = basename($_SERVER['REQUEST_URI']); // this still points to the jpg
if(is_file('folder/'. $filename) &&  is_allowed($user, $filename)) {
header('Content-Type: images/jpg'); // or other like .gif or .pdf etc
readfile('folder/'. $filename);
}
?>

最新更新