【问题标题】:Apache 2 + PHP - Issue with .htaccess permissionsApache 2 + PHP - .htaccess 权限问题
【发布时间】:2016-12-22 20:24:24
【问题描述】:

我创建了一个 apache2 环境来执行 PHP Web 应用程序。

几乎一切正常。

这是我的应用结构:

/var/www/html/
  - app/: PHP classes directory
  - views/: PHP templates directory
  - storage/: database and log stuffs directory
  - static/: public directory for js, css and images
  - index.php: Main script
  - .htaccess

下面的 .htaccess 文件

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule \.(static/)$ - [L]

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
</IfModule>

到 index.php 的重定向工作正常。我想只允许访问目录 static/ 并拒绝任何其他请求并将其重定向到 index.php。

这可能吗?

【问题讨论】:

    标签: php .htaccess apache2


    【解决方案1】:

    您可以在 .htaccess 中使用此代码:

    RewriteEngine On
    
    RewriteRule ^index\.php$ - [L,NC]
    
    RewriteCond %{THE_REQUEST} !\s/+static/ [NC]
    RewriteRule ^ index.php [L,NC]
    
    # remaining rules go here
    

    【讨论】:

    • 这个例子没有解决问题。 =/我仍然可以调用localhost/app/test.php
    • 对不起,我正在度假 =) 当我尝试访问 app/test.php 时,它会显示 test.php 的内容。重定向不起作用。
    • app/ 文件夹是否也有 .htaccess?您能出示您的完整 .htaccess 吗?
    • 重点是我不想为应用程序文件夹中的每个目录创建一个 .htaccess 文件。因为我只需要允许访问 /static 文件夹并将其他所有内容重定向到 /index.php
    • 我正在使用 Slim 3,我又试了一次,重定向工作正常。但我认为这不是正确的做法。 Tks 顺便说一句
    猜你喜欢
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2017-06-07
    • 2021-10-26
    • 2021-12-16
    • 2011-05-31
    相关资源
    最近更新 更多