Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 重定向或拒绝对公用文件夹的直接访问_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache 重定向或拒绝对公用文件夹的直接访问

Apache 重定向或拒绝对公用文件夹的直接访问,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我使用此htaccess直接访问public/css,public/js,public/images文件夹,它会重写从url中删除public: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [QSA,L] </IfModule> 重新启动发动机 重写基/ 重写规则^(css | j

我使用此htaccess直接访问
public/css
public/js
public/images
文件夹,它会重写从url中删除public:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /
    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [QSA,L]
</IfModule>

重新启动发动机
重写基/
重写规则^(css | js |图像)/(.*)$public/$1/$2[QSA,L]
例如,当访问此url时
http://localhost/css/file.css
显示
http://localhost/public/css/file.css


我想拒绝访问url
http://localhost/public/css/file.css
但允许
http://localhost/css/file.css
,可以这样做吗?

要解决此问题,您可以使用
%{the_REQUEST}
RewriteCond
,例如:

RewriteCond "%{THE_REQUEST}" "^GET\s/public/"
如果需要任何方法(
GET
POST
PUT
,等等),重定向例如:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /

    RewriteCond "%{THE_REQUEST}" "^[A-Z]+\s/public/"
    RewriteRule ^public/(.*)$ other-directory/$0 [QSA,L]

    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [QSA,L]
</IfModule>

重新启动发动机
重写基/
重写条件“%{THE_REQUEST}”“^[A-Z]+\s/public/”
重写规则^public/(*)$other目录/$0[QSA,L]
重写规则^(css | js |图像)/(.*)$public/$1/$2[QSA,L]
拒绝访问,例如:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /

    RewriteCond "%{THE_REQUEST}" "^[A-Z]+\s/public/"
    RewriteRule ^public/(.*)$ fake-directory/$0 [F]

    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [QSA,L]
</IfModule>

重新启动发动机
重写基/
重写条件“%{THE_REQUEST}”“^[A-Z]+\s/public/”
重写规则^public/(.*)$fake directory/$0[F]
重写规则^(css | js |图像)/(.*)$public/$1/$2[QSA,L]

我能问一下这有什么关系吗?无论哪种方式,您仍然可以访问css文件。这在源代码中。所有资产都将提供给客户。@PanamaJack感谢您的关注,没有特殊原因,这将是一个“规范URL”的问题。我想的问题是,用户从未看到URL,您可以使用robots.txt阻止对该文件夹进行爬网,但我只是没有看到资产文件夹上的要点,该文件夹必须可用才能正确显示页面。您自己解决得很好,祝您的项目好运。@PanamaJack谢谢!