【发布时间】:2017-05-25 19:03:28
【问题描述】:
我希望 apache 将所有内容重写到我的 index.php,这意味着 apache 不应该处理任何内容,它应该忽略 url 输入并将其转发到 index.php。
我已经搜索了很多次,我找到的最佳解决方案是:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
但今天我注意到,如果我输入现有路径,apache 将显示禁止页面,我不希望这样。我想要的是始终显示 index.php,无论 uri 是什么(显然是域名后面的部分:P)。
我正在使用$_SERVER['REQUEST_URI'] 让 PHP 使用 MVC 应用程序路由所有内容,因此 apache 尝试处理 url 输入非常冲突。
我禁用了索引,我也在考虑拒绝访问文档根目录并只允许 index.php 所在的文件夹:
httpd.conf:
<Directory "/srv/http">
Options -Indexes +FollowSymLinks
AllowOverride None
Require all denied
</Directory>
vhost.conf:
<VirtualHost *:80>
ServerName example.com
#Redirect permanent / http://www.example.com/
DocumentRoot "/srv/http/example.com/www"
<Directory "/srv/http/example.com/www">
Require all granted
<Files "index.php">
SetHandler "proxy:unix:/run/php-fpm/www.example.sock|fcgi://localhost/"
</Files>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
</Directory>
<IfModule dir_module>
DirectoryIndex index.php
</IfModule>
</VirtualHost>
我什至只想允许访问 index.php 文件,但也许这有点矫枉过正?
在 <Files> 块内移动 Require all granted 不起作用,但这是次要的,你可能会阻止我尝试(甚至可能是不可能的,或者是必要的)。
那么,如何防止 apache 处理 url?让我的 MVC 应用处理路由。
【问题讨论】:
标签: apache mod-rewrite url-rewriting