【问题标题】:Cannot get .htaccess file rewrite to work using a local dev server无法使用本地开发服务器重写 .htaccess 文件
【发布时间】:2014-09-22 19:57:08
【问题描述】:

我无法让我的 .htaccess 文件重写 URL。这是我确定的:

  • httpd.conf 文件有 LoadModule rewrite_module modules/mod_rewrite.so 未注释。
  • <Directory "C:/xampp/htdocs"> 下的httpd.conf 文件有AllowOverride All
  • phpinfo() 有 mod_rewrite 在加载的模块下。

我目前正在使用 XAMPP。

我的主目录是C:\xampp\htdocs\test

test 文件夹中,我有一个基本的index.php 文件和.htaccess 文件。

.htaccess 文件内:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$\ http://google.com [L]

#RewriteRule ^(.*)$\ index.php/$1 [L]
#RewriteBase /test/
  • .htaccess 文件正在被访问,因为当我在文件中扔垃圾时,当我访问我的 index.php 页面时,我得到一个 500 error
  • 底部两条注释规则是我打算使用的,但现在只要重定向到 google 就会让我知道它是否有效。我已经使用注释规则进行了尝试,但没有运气。
  • .htaccess 规则确实适用于我的 Web 服务器。

当我访问我的index.php 页面时,我没有被重定向。它只是正常加载页面。

我花了 4 个多小时寻找解决方案,但没有任何运气。我已经尝试过 XAMPP 和 EasyPHP,但两者的问题都是一样的。

如果有人有任何想法或建议,我将非常感激。

编辑:

以下是我在重新启动 PC 后启动 Apache 时的错误日志:

[Thu Jul 31 18:05:22.365119 2014] [ssl:warn] [pid 6380:tid 384] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Thu Jul 31 18:05:22.396117 2014] [core:warn] [pid 6380:tid 384] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Thu Jul 31 18:05:22.512122 2014] [ssl:warn] [pid 6380:tid 384] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Thu Jul 31 18:05:22.851147 2014] [mpm_winnt:notice] [pid 6380:tid 384] AH00455: Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11 configured -- resuming normal operations
[Thu Jul 31 18:05:22.851147 2014] [mpm_winnt:notice] [pid 6380:tid 384] AH00456: Apache Lounge VC11 Server built: Mar 16 2014 12:13:13
[Thu Jul 31 18:05:22.851147 2014] [core:notice] [pid 6380:tid 384] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
[Thu Jul 31 18:05:22.853142 2014] [mpm_winnt:notice] [pid 6380:tid 384] AH00418: Parent: Created child process 3936
[Thu Jul 31 18:05:23.241170 2014] [ssl:warn] [pid 3936:tid 416] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Thu Jul 31 18:05:23.388142 2014] [ssl:warn] [pid 3936:tid 416] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Thu Jul 31 18:05:23.423146 2014] [mpm_winnt:notice] [pid 3936:tid 416] AH00354: Child: Starting 150 worker threads.

【问题讨论】:

    标签: php apache .htaccess mod-rewrite xampp


    【解决方案1】:

    你的模式中的\ 把一切都搞砸了。实际发生的是\ 转义空间 这使得 mod_rewrite 认为模式 目标都是模式,[L] 标志是目标。 apache的指令和参数是用空格隔开的,所以当你把空格转义的时候,就让apache的东西整个就是一个参数了。

    你有什么:

    RewriteRule ^(.*)$\ http://google.com [L]
    |directive| | pattern               | | target |
    

    所以去掉\"

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://google.com [L]
    

    所以指令是RewriteRule,模式是^(.*)$,目标是http://google.com,标志是[L]

    【讨论】:

    • 你是对的,它不应该在那里,不知道我是怎么得到的。但即使在改变它之后,我仍然有同样的问题。还有其他想法吗?
    • 绝对没有:/
    • 实际上有一些错误,不确定是否相关。当我尝试访问该页面时,它们没有发生,只有在 Apache 启动时(在我重新启动我的 PC 之后)。我将它们发布为对我的问题的编辑。
    【解决方案2】:

    我能想到的另外两个原因:

    1. 您的 .htaccess 文件实际上是一个 .txt 文件,即“.htaccess.txt”,但“.txt”文件扩展名隐藏在文件资源管理器中。 (请参阅文件属性中的“文件类型:”字段以检查实际文件扩展名)
    2. 您的 .htaccess 文件没有被服务器读取的权限。

    备注:我已经检查过你的语法应该可以工作。

    【讨论】:

    • 1.我查了一下,文件类型是HTACCESS,不是txt。 2.服务器肯定可以访问,因为我在文件中扔垃圾字符时,我得到一个500 error
    • 您是否检查过没有其他 .conf 文件的“AllowOverride None”覆盖您的设置? (它们包含在主 httpd.conf 文件中,例如 ``Include path/to/*.conf'')
    • 是的,每个包含的 .conf 文件都将其 AllowOverride 设置为 All
    猜你喜欢
    • 1970-01-01
    • 2013-05-11
    • 2017-05-25
    • 2023-04-10
    • 1970-01-01
    • 2017-03-05
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多