如果 url 中的文件夹名称,则 .htaccess 重定向 [英] .htaccess redirect if folder name in url
问题描述
使用 .htaccess,我希望将 URL 从一个文件夹重定向到另一个文件夹.
using .htaccess, I'm looking to redirect a URL from one folder to another.
例如,如果用户请求forbiddenFolder"http://www.mysite.com/forbiddenFolder/subfolder
For example, if a user requests 'forbiddenFolder' http://www.mysite.com/forbiddenFolder/subfolder
或
http://www.mysite.com/forbiddenFolder/somefile.php
或
http://www.mysite.com/forbiddenFolder/somefile.swf
或
http://www.mysite.com/forbiddenFolder/somefile.f4v
.
他们应该被重定向到 http://www.mysite.com/forbidden.php
我不想锁定目录本身,因为那里有需要的资源.如果用户从 www.anothersite.com 创建脚本并从 创建 leeches 文件,则重定向应仅基于 URL 字符串工作http://www.mysite.com/forbiddenFolder
I don't want to lockdown the directory itself since there are resources there that are required. The redirect should only work based on the URL string OR if the user creates a script from www.anothersite.com and leeches files from http://www.mysite.com/forbiddenFolder
我认为应该实现重写条件......?
I believe rewrite conditions should be implemented....?
推荐答案
你可以试试这个:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/forbiddenFolder [NC]
RewriteCond %{REQUEST_URI} /[w]+.(php|swf|f4v) [NC,OR]
RewriteCond %{REQUEST_URI} /[^.]+/?$ [NC]
RewriteCond %{REQUEST_URI} !forbidden.php [NC]
RewriteRule .* /forbidden.php [L,NC]
内部映射
http://www.mysite.com/forbiddenFolder/subfolder
或
http://www.mysite.com/forbiddenFolder/somefile.php
或
http://www.mysite.com/forbiddenFolder/somefile.swf
或
http://www.mysite.com/forbiddenFolder/somefile.f4v
致:
http://www.mysite.com/forbidden.php
对于永久可见的重定向,将 [L,NC]
替换为 [R=301,L,NC]
For permanent and visible redirection, replace [L,NC]
with [R=301,L,NC]
具有修改后的传入 URL 结构.
With a modified incoming URL structure.
例如:
http://localhost/trafficticket.tv/Sites/public_html/forbiddenFolder/somefile.swf
用这个替换之前的规则块:
Replace the previous block of rules with this one:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /forbiddenFolder [NC]
RewriteCond %{REQUEST_URI} /[w]+.(php|swf|f4v) [NC,OR]
RewriteCond %{REQUEST_URI} /[^.]+/?$ [NC]
RewriteCond %{REQUEST_URI} !forbidden.php [NC]
RewriteRule .* /trafficticket.tv/Sites/public_html/forbidden.php [L,NC]
要使用任何其他 URL,唯一的要求是在重写规则中将正确的段路径添加到 /forbidden.php
.在这种情况下,该段是
To use any other URL, the only requirement is to prepend the correct segment path to /forbidden.php
in the rewrite rule. In this case, that segment is
/trafficticket.tv/Sites/public_html
但它可以是任何东西也可以什么都不是,就像在原始问题中一样.
but it could be anything or nothing, like in the original question.
这篇关于如果 url 中的文件夹名称,则 .htaccess 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!