启用mod_rewrite,重写引擎处于打开状态,但在没有扩展名的情况下不会加载文件.php



我正在使用运行PHP 5.6的Apache 2.4 httpd服务器

我通过删除以下评论在 httpd.conf 文件夹中启用了mod_rewrite: LoadModule rewrite_module modules/mod_rewrite.so

我已将 .htaccess 文件添加到我的服务器目录和虚拟主机目录中。

服务器目录位于httpd.conf文件中,如下所示:

DocumentRoot "c:/web"
<Directory "c:/web">
    DirectoryIndex index.html index.php
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    Allow from All
</Directory>

在我的httpd-vhosts.conf中,我有以下内容:

ServerName localhost
DocumentRoot "c:web"
<VirtualHost *:80>
    <Directory "c:web">
        Options Indexes FollowSymLinks MultiViews Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
    ServerName localhost
    ServerAlias localhost
    DocumentRoot "c:web"
</VirtualHost>
<VirtualHost *:80>
    ServerName Personal.localhost
    ServerAlias Personal.localhost
    DocumentRoot "C:personalweb"
    DirectoryIndex index.html index.php
    <Directory "C:personalweb">
        Options Indexes FollowSymLinks MultiViews Includes ExecCGI
        AllowOverride All
        Order Allow,Deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

我知道 Apache 服务器加载.htaccess文件,因为如果我将损坏的代码添加到文件中,我会得到一个500 Internal Server Error(当损坏的代码位于页面顶部或底部时,它会这样做。

我的.htaccess文件如下所示:

IndexIgnore * # prevent directory listing
Order deny,allow
Allow from *
# ------------------------------------------
# Hide the PHP extentions
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
# ------------------------------------------
# This should set cache for all items
<IfModule mod_headers.c>
    <FilesMatch ".(jpg|jpeg|png|gif|swf|mp3|avi|js|css|html|php)$">
        Header set Cache-Control "max-age=604800, public"
    </FilesMatch>
</IfModule>
# ------------------------------------------
# Compress data
<ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

access.log文件中,我可以看到请求,但它没有将.php扩展添加到 URL 的末尾:

DeveloperUsr - - [27/Jan/2016:15:12:44 +0200] "GET /admin/login HTTP/1.1" 404 209

error.log文件具有以下内容: [Wed Jan 27 15:12:44.324658 2016] [negotiation:error] [pid 18324:tid 1084] [client ::1:50784] AH00687: Negotiation: discovered file(s) matching request: C:/Personal/web/admin/login (None could be negotiated).

我该怎么做才能启用.htaccess或 URL 重写。我知道.htaccess文件有效,因为如果我将服务器加载到 Linux 生产环境,它会加载并工作。我无权访问生产环境的配置文件,因此我看不到我的配置文件出了什么问题。

如果您依赖mod_rewrite以这种方式进行映射,请禁用多视图。

最新更新