apache .htaccess 中的 RewriteRule 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2194137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RewriteRule in .htaccess not working
提问by riwalk
I am currently running Apache2 on my local machine, installed with the latest version of Ubuntu.
我目前在我的本地机器上运行 Apache2,安装了最新版本的 Ubuntu。
I am trying to get basic URL rewriting working by using the .htaccess file.
我正在尝试使用 .htaccess 文件进行基本的 URL 重写。
The file "http://localhost/page.php?=home" does exist, and the location "/doesnotexist/home" does not.
文件“ http://localhost/page.php?=home”确实存在,而位置“/doesnotexist/home”不存在。
I would like to have the first page be loaded when the second is requested.
我希望在请求第二页时加载第一页。
My .htaccess file looks like this:
我的 .htaccess 文件如下所示:
RewriteEngine On
RewriteRule ^/doesnotexist/(.*)$ /page.php?p=
My httpd.conf file looks like this:
我的 httpd.conf 文件如下所示:
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
<Directory /var/www>
AllowOverride All
</Directory>
Note that my httpd.conf file looks exactlylike that, as it was empty before I edited it.
请注意,我的 httpd.conf 文件看起来完全一样,因为在我编辑它之前它是空的。
The result that I get is this:
我得到的结果是这样的:
Not Found
The requested URL /doesnotexist/home was not found on this server.
I have googled the ever living **** out of this problem, and I have never gotten anything other than the error above.
我已经用谷歌搜索了这个问题的永远存在的 ****,除了上面的错误,我从来没有得到任何东西。
If anyone has any ideas, I would be very appreciative.
如果有人有任何想法,我将不胜感激。
回答by riwalk
For the benefit of others, I figured out the answer:
为了他人的利益,我想出了答案:
In the file "/etc/apache2/sites-enabled/000-default" there was the line:
在文件“/etc/apache2/sites-enabled/000-default”中有一行:
AllowOverride None
Change this to:
将此更改为:
AllowOverride All
回答by Gumbo
You need to remove the contextual path prefix from your pattern when using mod_rewrite in a .htaccess file. In the case of the root directory, the path prefix is just /. So try this:
在 .htaccess 文件中使用 mod_rewrite 时,您需要从模式中删除上下文路径前缀。在根目录的情况下,路径前缀只是/. 所以试试这个:
RewriteRule ^doesnotexist/(.*)$ /page.php?p=
回答by Varun Bhatia
If I place a .htaccess into /Library/WebServer/Documents and open "localhost/"; to test it, this works as expected. It just doesn't work in "~/Sites". I have tried this on Mac OS X Mavericks.
如果我将 .htaccess 放入 /Library/WebServer/Documents 并打开“localhost/”;测试它,这按预期工作。它只是在“〜/ Sites”中不起作用。我在 Mac OS X Mavericks 上试过这个。

