【问题标题】:Difficulty in accessing sub directories files because of htaccess由于 htaccess,难以访问子目录文件
【发布时间】:2016-06-14 04:56:07
【问题描述】:

这是我的.htaccess 代码

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*)$ api.php [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -s
    RewriteRule ^(.*)$ api.php [QSA,NC,L]

    RewriteCond %{REQUEST_URI} !^/images/   

</IfModule>

我的目录结构:

我的问题是我可以通过调用/users/getData 等访问我的api.php。但是如果我去images/someImage.jpg 那么它不允许。

【问题讨论】:

    标签: php .htaccess web


    【解决方案1】:

    试试这个简单的重写规则

    RewriteBase /your web directory
    

    RewriteEngine On

    RewriteRule ^([a-zA-Z0-9_-]+)$ api.php?rquest=$1

    RewriteRule ^([a-zA-Z0-9_-]+)/$ api.php?rquest=$1

    以下是分解代码

    RewriteBase / #enter your directory root here

    RewriteEngine On

    以下重写规则会将例如 www.example.com/home 重定向到 example.com/api.php?rquest=$1

    RewriteRule ^([a-zA-Z0-9_-]+)$ api.php?rquest=$1

    以下内容将重定向例如 www.example/home/ 到 example.com/api.php?rquest=$1。注意 /home 之后的 /

    RewriteRule ^([a-zA-Z0-9_-]+)/$ api.php?rquest=$1
    

    上述代码行排除了将 www.example.com/images/someimages.jpg 重定向到 www.example.com/ 的规则

    所以这现在可以工作了。

    【讨论】:

    • RewriteRule ^([a-zA-Z0-9_-]+)/$ api.php?rquest=$1 我也试过这个,但它仍然允许子目录文件
    • 也许我一开始没有得到这个问题。你能解释一下吗,我可以帮忙
    • 假设我将 URl 设为 www.mydomain.com/login 那么它将调用登录方法 api.php,如果 www.mydomain.com/users 它将调用 api.php 中的用户方法我们做了“RewriteRule ^([a-zA-Z0-9_-]+)/$ api.php?rquest=$1” 但没有因为这条规则我不能访问 www.mydomain.com/images/some。 jpg
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2019-03-03
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    相关资源
    最近更新 更多