【问题标题】:.htaccess from root directory prevents access to subdirectory php files.htaccess 从根目录阻止访问子目录 php 文件
【发布时间】:2023-04-01 10:35:01
【问题描述】:

我在根目录中安装了 Magento,在这样的子目录中安装了一些独立脚本

app
index.php
...
.htaccess
scripts/
├── csv-generator
├── index.html
├── index.php
├── label-print
└── stock-manage

我的问题是,当我尝试访问 /scripts 目录中的 php 或 html 文件时,根目录中的 .htaccess 似乎会启动并启动我的 magento 应用程序而不是子目录脚本

docroot .htaccess

############################################
## GoDaddy specific options

#   Options -MultiViews

## you might also need to add this line to php.ini
##     cgi.fix_pathinfo = 1
## if it still doesn't work, rename php.ini to php7.ini

############################################
## default index file

    DirectoryIndex index.php

############################################
## php7 settings

<IfModule mod_php7.c>

############################################
## adjust max execution time

    php_value max_execution_time 18000

############################################
## disable automatic session start
## before autoload was initialized

    php_flag session.auto_start off

############################################
## enable resulting html compression

    #php_flag zlib.output_compression on

###########################################
# disable user agent verification to not break multiple image upload

    php_flag suhosin.session.cryptua off

</IfModule>

<IfModule mod_security.c>
###########################################
# disable POST processing to not break multiple image upload

    SecFilterEngine Off
    SecFilterScanPOST Off
</IfModule>

<IfModule mod_deflate.c>

############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip

    # Insert filter on all content
    ###SetOutputFilter DEFLATE
    # Insert filter on selected content types only
    #AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript

    # Netscape 4.x has some problems...
    #BrowserMatch ^Mozilla/4 gzip-only-text/html

    # Netscape 4.06-4.08 have some more problems
    #BrowserMatch ^Mozilla/4\.0[678] no-gzip

    # MSIE masquerades as Netscape, but it is fine
    #BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

    # Don't compress images
    #SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

    # Make sure proxies don't deliver the wrong content
    #Header append Vary User-Agent env=!dont-vary

</IfModule>

<IfModule mod_ssl.c>

############################################
## make HTTPS env vars available for CGI mode

    SSLOptions StdEnvVars

</IfModule>

<IfModule mod_rewrite.c>

############################################
## enable rewrites

    Options +FollowSymLinks
    RewriteEngine on


############################################
## you can put here your magento root folder
## path relative to web root

    #RewriteBase /magento/

############################################
## uncomment next line to enable light API calls processing

#    RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type=$1 [QSA,L]

############################################
## rewrite API2 calls to api.php (by now it is REST only)

    RewriteRule ^api/rest api.php?type=rest [QSA,L]

############################################
## workaround for HTTP authorization
## in CGI environment

    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

############################################
## TRACE and TRACK HTTP methods disabled to prevent XSS attacks

    RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
    RewriteRule .* - [L,R=405]

<IfModule mod_setenvif.c>

############################################
## Enable Developer Mode based on OS environment variable

    SetEnvIfExpr "osenv('MAGE_IS_DEVELOPER_MODE') == '1'" MAGE_IS_DEVELOPER_MODE=1

    <IfModule mod_headers.c>

        ############################################
        # X-Content-Type-Options: nosniff disable content-type sniffing on some browsers.
        Header set X-Content-Type-Options: nosniff

        ############################################
        # This header forces to enables the Cross-site scripting (XSS) filter in browsers (if disabled)
        BrowserMatch \bMSIE\s8 ie8
        Header set X-XSS-Protection: "1; mode=block" env=!ie8

    </IfModule>
</IfModule>

############################################
## redirect for mobile user agents

    #RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
    #RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
    #RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]


############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l


############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]

</IfModule>


############################################
## Prevent character encoding issues from server overrides
## If you still have problems, use the second line instead

    AddDefaultCharset Off
    #AddDefaultCharset UTF-8

<IfModule mod_expires.c>

############################################
## Add default Expires header
## http://developer.yahoo.com/performance/rules.html#expires
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>

############################################
## By default allow all access

    Order allow,deny
    Allow from all

###########################################
## Deny access to release notes to prevent disclosure of the installed Magento version

    <Files RELEASE_NOTES.txt>
        order allow,deny
        deny from all
    </Files>

############################################
## If running in cluster environment, uncomment this
## http://developer.yahoo.com/performance/rules.html#etags

    #FileETag none

###########################################
## Deny access to cron.php
    <Files cron.php>

############################################
## uncomment next lines to enable cron access with base HTTP authorization
## http://httpd.apache.org/docs/2.2/howto/auth.html
##
## Warning: .htpasswd file should be placed somewhere not accessible from the web.
## This is so that folks cannot download the password file.
## For example, if your documents are served out of /usr/local/apache/htdocs
## you might want to put the password file(s) in /usr/local/apache/.

        #AuthName "Cron auth"
        #AuthUserFile ../.htpasswd
        #AuthType basic
        #Require valid-user

############################################

        Order allow,deny
        Deny from all

    </Files>

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74___lsphp .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

子目录.htaccess

Authtype Basic
AuthName "Restricted area"
AuthUserFile "/home/passwd"
Require valid-user

好像是指令

RewriteRule .* index.php [L]

正在处理所有请求,我正在尝试找到一种方法来忽略脚本目录,以便它正常工作。任何帮助表示赞赏。

【问题讨论】:

  • 在 index.php 重写之前放置一个RewriteRule ^scripts/ - [END]
  • 不幸的是没有工作
  • 你把它放在哪里了,到底发生了什么?
  • 我把它直接放在RewriteRule .* index.php [L]上面 网站工作正常但是scripts目录又回到了网站的404页面
  • 把它放在第一位会发生什么? “但是scripts目录又回到了网站的404页面”——当你请求目录本身,还是其中的特定文件?

标签: apache .htaccess magento mod-rewrite


【解决方案1】:

@CBroe:在 index.php 重写之前放置一个RewriteRule ^scripts/ - [END]

@OP:我直接放在RewriteRule .* index.php [L]上面


############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l


############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]

这是一条规则(只是“令人困惑地”拆分)。您不应在上述RewriteRule 指令之前添加另一个RewriteRule 指令immediatley,因为这可能会破坏您的网站。当声明“在 index.php 重写之前”放置一些东西时,它意味着 before 整个规则,即。在适用于该规则的第一个 RewriteCond 指令之前。

但是,在上面,您可以将scripts 添加到第一个RewriteCond 指令中。

例如:

RewriteCond %{REQUEST_URI} !^/(media|skin|js|scripts)/

条件可防止在请求这些根目录中的任何一个时处理规则。

但是,通常情况下,该规则自然会排除对物理文件和目录的请求(这就是中间的 3 个条件所做的)。但是,由于某种原因,这似乎不会发生。

【讨论】:

  • 确实这是我第一次遇到这种情况,这也是我问的原因。我尝试了您在 RewriteCond 中添加脚本路径以排除它的建议,但是行为没有任何变化。更具体地说,无论我在 URL 中尝试 example.com/scripts/index.php 还是简单的 example.com/scripts,我仍然得到 404 知道可能会干扰什么或在哪里查看更多信息?
  • @gabtzi 当您最初将该规则放在“RewriteRule .* index.php [L] 的正上方”时 - 是不是就在该指令的正上方?只是那应该破坏了magento。如果没有,那么我们需要寻找其他地方。
  • 是的,我确实把它放在了这条规则之上,我也在最上面尝试过。无论我尝试什么,结果都保持不变。注释掉这条规则,Magento 就会中断,并且脚本可以工作。
  • 不太确定在这种情况下发生了什么?!您是否还有其他.htaccess 文件,可能在/scripts 子目录中?您在代理/负载均衡器/CDN 后面吗?好像当您请求/scripts/ 时,Apache 没有看到?我很想知道REQUEST_URI 服务器变量在脚本中的值是多少?还是 RewriteRule 指令匹配的 URL 路径?因为两者都失败了。 (请注意,REQUEST_URI 服务器变量与 PHP 的 $_SERVER['REQUEST_URI'] 超全局变量不同。)
  • Scripts 目录只有基本的 auth .htaccess 代码,没有别的。我们正在使用 cloudflare,但这似乎不是原因。不幸的是,我没有完全访问 VPS 的权限,因为它是托管的,所以我无法获得该信息 :( 现在我要求托管服务提供商切换到 litespeed,一切似乎都运行良好,但这让我感到困惑非常了解正在发生的事情,并且我已经为各种 .htaccess 配置苦苦挣扎了好几天。就好像父 .htaccess 正在重写子目录中的所有类似选项
猜你喜欢
  • 2011-09-16
  • 2012-08-22
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 2012-07-30
  • 2014-03-31
相关资源
最近更新 更多