【问题标题】:.htaccess Messing up with .php extension?.htaccess 搞砸了 .php 扩展名?
【发布时间】:2011-08-09 02:27:59
【问题描述】:

好的,所以我有这个 mod_rewrite 规则,如果在发送请求时没有提供它,它会在内部附加一个 .html 扩展名:

选项 +FollowSymLinks 重写引擎开启 # ## 在内部将无扩展名文件请求重写为 .html 文件 ## # # 如果请求的 URI 在最终路径部分中不包含句点 RewriteCond %{REQUEST_URI} !(\.[^./]+)$ # 如果它不作为目录存在 RewriteCond %{REQUEST_FILENAME} !-d # 如果它不作为文件存在 RewriteCond %{REQUEST_FILENAME} !-f # 然后添加 .html 以获取实际文件名 重写规则 (.*) /$1.html [L] # # ## 从外部将直接请求 .html 页面 URI 的客户端重定向到无扩展 URI # # 如果客户端请求头包含html文件扩展名 RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+html\ HTTP # 外部重定向到无扩展 URI 重写规则 ^(.+)\.html$ http://www.example.com/$1 [R=301,L]

我考虑过在这个文件的第一部分将任何地方说 .html 更改为 .php,这样当有人请求一个 .php 文件而不添加它的扩展名时,它仍然会转到那个 .php 页面。事实证明,这行不通。为什么不呢?

我的证据在这里:http://appstorecrazy.com/OHNOEZ/NOTEST

【问题讨论】:

  • 因为你的根目录下没有文件NOTEST.php

标签: php .htaccess


【解决方案1】:

尝试以下处理php的规则:

## Internally rewrite extensionless file requests to .php files ##
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# and if it does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# then add .html to get the actual filename
RewriteRule (.*) /$1.php [L]

## Externally redirect clients directly requesting .php page URIs to extensionless URIs
#
# If client request header contains html file extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.php
# externally redirect to extensionless URI
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

【讨论】:

  • 如果您可以添加更多解释会更好,例如 RewriteCond 中第三个语句的作用是什么,以及为什么它有 2 个正则表达式模式
猜你喜欢
  • 2014-06-20
  • 2012-12-07
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 2012-09-20
  • 2014-04-28
  • 2012-06-08
  • 1970-01-01
相关资源
最近更新 更多