【问题标题】:URL Rewrite: Prevent rewrite entirely on exising resourceURL 重写:完全防止重写现有资源
【发布时间】:2019-04-07 17:28:04
【问题描述】:

我的根目录下面的 .htaccess 文件用于重写所有 /api/ 然后从 /api/api.php 中的 $_GET 的 URL 中输出键值对?

我该如何修改这个,让所有在api中的现有文件或文件夹(例如通过这个方案访问的/api/test或/api/test/file.php不会被重写规则重写?

<IfModule mod_rewrite.c> 
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/api
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #RewriteRule (.*) $1
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&%1 [QSA,L]
    RewriteCond %{REQUEST_URI} ^/api
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^([^/]+)/ $1/$1.php?%1 [L]
</IfModule>

【问题讨论】:

    标签: php apache mod-rewrite


    【解决方案1】:

    您可以使用RewriteCond 指令来排除您的文件和文件夹

    RewriteEngine On
    #if not a file
    RewriteCond %{REQUEST_FILENAME} !-f
    #not a directory
    RewriteCond %{REQUEST_FILENAME} !-d
    #rewrite the /api request
    RewriteCond %{REQUEST_URI} ^/api
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^([^/]+)/ $1/$1.php?%1 [L]
    

    【讨论】:

    • 谢谢starkeen,应该!-f 读取!-d 用于第二个重写条件?
    【解决方案2】:

    我制作了以下 .htaccess 文件以包含目录索引和备用资源,并将其放在根目录中以捕获来自根的每个请求。

    我还设置了 rewritebase /api 和 InheritDown 选项来限制我应用这些规则的位置,并递归地向下传播到已经存在的目录,以便它们(和嵌套文件)在请求时不会包含在重写过程中。

    这在 $_GET 上产生了一个成功的结果,其中查询字符串中有相同数量的 lhs(键)= rhs(值)对,前提是它们在请求。

     <IfModule mod_rewrite.c> 
        Options +FollowSymlinks
        RewriteEngine On
        DirectoryIndex api.php
        FallbackResource index.php
        RewriteCond %{REQUEST_URI} ^/api
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_URI} ^/api
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{QUERY_STRING} ^(.*)$
        RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&%1 [QSA,L]
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{QUERY_STRING} ^(.*)$
        RewriteRule ^([^/]+)/ $1/$1.php?%1 [L]
    </IfModule>
    
    RewriteBase /api
    RewriteOptions InheritDown
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      相关资源
      最近更新 更多