【问题标题】:Rewrite requests to a directory (w/o a trailing slash) to index.php in .htaccess将对目录的请求(不带斜杠)重写为 .htaccess 中的 index.php
【发布时间】:2013-06-28 17:36:01
【问题描述】:
我已经看到了类似的要求,但无法找到有效的硬性和快速的答案。本质上,我想在内部将对该目录的请求(请求的 URL 不包含尾部斜杠)重写为该目录中包含的 index.php:
http://example.com/foo => http://example.com/foo/index.php
现在,如果向 http://example.com/foo 发出请求,apache 将执行外部可见的 301 重定向到 http://example.com/foo/,这反过来会导致 index.php 被渲染,但我想避免这种外部重定向和额外的请求。
【问题讨论】:
标签:
apache
.htaccess
mod-rewrite
【解决方案1】:
通过httpd.conf启用mod_rewrite和.htaccess,然后把这段代码放到你.htaccess的DOCUMENT_ROOT目录下:
DirectorySlash On
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (?!^.+?/$)^(.+)$ /$1/index.php [L]