无法使用 XAMPP 设置 mod_rewrite

无法使用 XAMPP 设置 mod_rewrite

我正在尝试在 XAMPP 5.6.3 安装上启用 mod_rewrite。我一直遵循以下指南:

...我设法在 Google 上搜索了其他一些内容。不幸的是,毫无结果。

我有一个在 Linux 下开发的项目,在那里运行良好,.htaccess里面有文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

我努力了:

  1. 确保该行LoadModule rewrite_module modules/mod_rewrite.so未被注释httpd.conf
  2. AllowOverride noneAllowOverride allin替换httpd.conf(出现 2 或 3 次)。
  3. 当上述方法不起作用时,在我添加的所有部分中添加AllowOverride all指令,以防万一。<Directory>httpd-vhosts.conf
  4. 更改#2 和#3 后重新启动。

我尝试使用 .htaccess 文件访问目录中的 URL,但可能没有一个 URL 被重写,因此出现 404 错误。访问应重写的文件是可能的,并且不会出现任何错误。访问.htaccess本身会出现 403 错误。

我也尝试添加:

RewriteLog "C:/XAMPP/apache/logs/rewrite.log"
RewriteLogLevel 3

...同时使用httpd.confhttpd-vhosts.conf,有时不使用RewriteLog,但是,当我这样做时,Apache 拒绝启动 - 有时它会向 XAMPP 控制台吐出一般错误消息,有时它会挂起。但它不会写入任何内容error.log。因此,没有mod_rewrite调试输出。phpinfo()但是,显示已加载的模块是:

核心 mod_win32 mpm_winnt http_core mod_so mod_access_compat mod_actions mod_alias mod_allowmethods mod_asis mod_auth_basic mod_authn_core mod_authn_file mod_authz_core mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_dav_lock mod_dir mod_env mod_headers mod_include mod_info mod_isapi mod_log_config mod_cache_disk mod_mime mod_negotiation mod_proxy mod_proxy_ajp mod_rewrite mod_setenvif mod_socache_shmcb mod_ssl mod_status mod_version mod_php5

因此它应该或多或少处于正常工作状态。

这让我抓狂。请帮帮我。

以下是一些看起来有点用处的内容:

httpd.conf:

ServerRoot "C:/XAMPP/apache"


Listen 80

LoadModule access_compat_module modules/mod_access_compat.so
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule allowmethods_module modules/mod_allowmethods.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule dav_lock_module modules/mod_dav_lock.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule include_module modules/mod_include.so
LoadModule info_module modules/mod_info.so
LoadModule isapi_module modules/mod_isapi.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule cache_disk_module modules/mod_cache_disk.so
LoadModule mime_module modules/mod_mime.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule status_module modules/mod_status.so
LoadModule version_module modules/mod_version.so

<IfModule unixd_module>
User daemon
Group daemon

</IfModule>


ServerAdmin postmaster@localhost

ServerName localhost:80

<Directory />
    AllowOverride all
    Require all denied
</Directory>


DocumentRoot "C:/XAMPP/htdocs"
<Directory "C:/XAMPP/htdocs">
    Options Indexes FollowSymLinks Includes ExecCGI

    AllowOverride All

    Require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
                   default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
                   home.php home.pl home.cgi home.asp home.shtml home.html home.htm
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error.log"

LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>


    CustomLog "logs/access.log" combined
</IfModule>

<IfModule alias_module>


    ScriptAlias /cgi-bin/ "C:/XAMPP/cgi-bin/"

</IfModule>

<IfModule cgid_module>
</IfModule>

<Directory "C:/XAMPP/cgi-bin">
    AllowOverride All
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig conf/mime.types

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    AddHandler cgi-script .cgi .pl .asp


    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

<IfModule mime_magic_module>
    MIMEMagicFile "conf/magic"
</IfModule>






Include conf/extra/httpd-mpm.conf

Include conf/extra/httpd-multilang-errordoc.conf

Include conf/extra/httpd-autoindex.conf

Include conf/extra/httpd-languages.conf

Include conf/extra/httpd-userdir.conf

Include conf/extra/httpd-info.conf

Include conf/extra/httpd-vhosts.conf



Include "conf/extra/httpd-proxy.conf"
Include "conf/extra/httpd-default.conf"
Include "conf/extra/httpd-xampp.conf"

<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>

Include conf/extra/httpd-ssl.conf
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>


AcceptFilter http none

<IfModule mod_proxy.c>
<IfModule mod_proxy_ajp.c>
Include "conf/extra/httpd-ajp.conf"
</IfModule>
</IfModule>

httpd-vhosts.conf:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "S:/efit_rails4/public"
    Alias /remote-control "S:/ssm-misc/remote-control"
    <Directory "S:/efit_rails4/public">
        Require all granted
        AllowOverride All
    </Directory>
    <Directory "S:/ssm-misc/remote-control">
        Require all granted
        AllowOverride All
    </Directory>
    ServerName SSM
</VirtualHost>

error.log 的底部:

[Fri Nov 28 05:43:56.875099 2014] [mpm_winnt:notice] [pid 9780:tid 380] AH00456: Apache Lounge VC11 Server built: Jul 17 2014 11:50:08
[Fri Nov 28 05:43:56.875099 2014] [core:notice] [pid 9780:tid 380] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/XAMPP/apache'
[Fri Nov 28 05:43:56.876100 2014] [mpm_winnt:notice] [pid 9780:tid 380] AH00418: Parent: Created child process 8276
[Fri Nov 28 05:43:57.155297 2014] [ssl:warn] [pid 8276:tid 384] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Fri Nov 28 05:43:57.255380 2014] [ssl:warn] [pid 8276:tid 384] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Fri Nov 28 05:43:57.280385 2014] [mpm_winnt:notice] [pid 8276:tid 384] AH00354: Child: Starting 150 worker threads.
[Fri Nov 28 05:44:18.865136 2014] [authz_core:error] [pid 8276:tid 1728] [client 127.0.0.1:63898] AH01630: client denied by server configuration: S:/ssm-misc/remote-control/api/.htaccess

更新

经过一番劝说(以及@AD7six)我能够让调试输出正常工作。似乎有一种较新的方法来配置它:

RewriteBase /remote-control/api/

我对重写模块感到很奇怪,它似乎正确地重写了 URL,然后立即提供 404。此外,我尝试进行实验并发现,如果我添加一个等于当前目录的显式重写基数,例如:

RewriteBase /remote-control/api/

重写工作正常 - 我得到了 index.php 服务,但我遇到了其他错误,所以我不能完全确认它是否确实有效。但是,我对这个解决方案并不完全满意。此外,在这种情况下,调试输出有点长,好像它正在再进行一次。最后,它得到了一个相同的文件名,但没有 404:

[Thu Dec 04 00:37:31.417478 2014] [rewrite:trace3] [pid 5636:tid 1728] mod_rewrite.c(475): [client ::1:58668] ::1 - - [localhost/sid#f51730][rid#88d4118/initial] [perdir S:/ssm-misc/remote-control/api/] strip per-dir prefix: S:/ssm-misc/remote-control/api/session -> session
[Thu Dec 04 00:37:31.417478 2014] [rewrite:trace3] [pid 5636:tid 1728] mod_rewrite.c(475): [client ::1:58668] ::1 - - [localhost/sid#f51730][rid#88d4118/initial] [perdir S:/ssm-misc/remote-control/api/] applying pattern '^' to uri 'session'
[Thu Dec 04 00:37:31.417478 2014] [rewrite:trace2] [pid 5636:tid 1728] mod_rewrite.c(475): [client ::1:58668] ::1 - - [localhost/sid#f51730][rid#88d4118/initial] [perdir S:/ssm-misc/remote-control/api/] rewrite 'session' -> 'index.php'
[Thu Dec 04 00:37:31.417478 2014] [rewrite:trace3] [pid 5636:tid 1728] mod_rewrite.c(475): [client ::1:58668] ::1 - - [localhost/sid#f51730][rid#88d4118/initial] [perdir S:/ssm-misc/remote-control/api/] add per-dir prefix: index.php -> S:/ssm-misc/remote-control/api/index.php
[Thu Dec 04 00:37:31.417478 2014] [rewrite:trace2] [pid 5636:tid 1728] mod_rewrite.c(475): [client ::1:58668] ::1 - - [localhost/sid#f51730][rid#88d4118/initial] [perdir S:/ssm-misc/remote-control/api/] trying to replace prefix S:/ssm-misc/remote-control/api/ with /remote-control/api/
[Thu Dec 04 00:37:31.417478 2014] [rewrite:trace1] [pid 5636:tid 1728] mod_rewrite.c(475): [client ::1:58668] ::1 - - [localhost/sid#f51730][rid#88d4118/initial] [perdir S:/ssm-misc/remote-control/api/] internal redirect with /remote-control/api/index.php [INTERNAL REDIRECT]
[Thu Dec 04 00:37:31.417478 2014] [rewrite:trace3] [pid 5636:tid 1728] mod_rewrite.c(475): [client ::1:58668] ::1 - - [localhost/sid#f51730][rid#88d7bb8/initial/redir#1] [perdir S:/ssm-misc/remote-control/api/] strip per-dir prefix: S:/ssm-misc/remote-control/api/index.php -> index.php
[Thu Dec 04 00:37:31.417478 2014] [rewrite:trace3] [pid 5636:tid 1728] mod_rewrite.c(475): [client ::1:58668] ::1 - - [localhost/sid#f51730][rid#88d7bb8/initial/redir#1] [perdir S:/ssm-misc/remote-control/api/] applying pattern '^' to uri 'index.php'
[Thu Dec 04 00:37:31.418478 2014] [rewrite:trace1] [pid 5636:tid 1728] mod_rewrite.c(475): [client ::1:58668] ::1 - - [localhost/sid#f51730][rid#88d7bb8/initial/redir#1] [perdir S:/ssm-misc/remote-control/api/] pass through S:/ssm-misc/remote-control/api/index.php

这到底发生了什么事?

答案1

RewriteRule错了。它应该是这样的:

  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ /index.php [L,QSA]

调整重定向路径以适应,但您需要的不仅仅是 ^ 字符。

相关内容