【问题标题】:htaccess rewrite, how to negate sub directory fileshtaccess重写,如何否定子目录文件
【发布时间】:2014-02-11 01:37:32
【问题描述】:

我有执行 URL 重写的 htaccess 文件。它基本上删除了 .php 扩展名。

规则工作正常,例如他们成功地将http://example.com/contact.php 改写为http://example.com/contact

但是重写也会更改子目录文件,例如/includes/check-form-input.php

因此,例如,当contact.php 的表单尝试提交到/includes/check-form-input.php 时,htaccess 重写会从末尾剥离 .php,我得到 404 个标题。
/includes/check-form-input 未找到。

至少看起来是这样,使用HTTP_REFERER 似乎证实了这一点,因为引用者是/includes/check-form-input 没有分机.php

有没有办法添加一个规则来否定所有子目录?

我有以下 htaccess - 这是从 SO 上的另一个答案复制而来的,因为我没有 100% 掌握重写结构。
(如果这是原因,我已经离开 www 到非 www 重定向)

## Rewrite on
RewriteEngine on


## Redirect all pages from non-www to www
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]


## URL rewrite
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您可以排除所有子目录,

    # Redirect external .php requests to extensionless url
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^([^/]+)\.php$ http://example.com/$1 [R=301,L]
    

    或将其限制为仅 GET 请求:

    # Redirect external .php requests to extensionless url
    RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ (.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.php$ http://example.com/$1 [R=301,L]
    

    或者只是排除 includes 子目录

    # Redirect external .php requests to extensionless url
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteCond %{REQUEST_URI} !^/includes/
    RewriteRule ^([^/]+)\.php$ http://example.com/$1 [R=301,L]
    

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 2021-03-13
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多