.htaccess redirect if folder name in url

2020-03-24 03:58发布

using .htaccess, I'm looking to redirect a URL from one folder to another.

For example, if a user requests 'forbiddenFolder' http://www.mysite.com/forbiddenFolder/subfolder

or

http://www.mysite.com/forbiddenFolder/somefile.php or

http://www.mysite.com/forbiddenFolder/somefile.swf or

http://www.mysite.com/forbiddenFolder/somefile.f4v.

they should be redirected to http://www.mysite.com/forbidden.php

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

1条回答
家丑人穷心不美
2楼-- · 2020-03-24 04:58

You may try this:

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]  

Maps internally

http://www.mysite.com/forbiddenFolder/subfolder or

http://www.mysite.com/forbiddenFolder/somefile.php or

http://www.mysite.com/forbiddenFolder/somefile.swf or

http://www.mysite.com/forbiddenFolder/somefile.f4v

To:

http://www.mysite.com/forbidden.php

For permanent and visible redirection, replace [L,NC] with [R=301,L,NC]


OPTION

With a modified incoming URL structure.

For example:

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]

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.

查看更多
登录 后发表回答