php 如何激活 mod_rewrite?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8628907/
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
How to activate mod_rewrite?
提问by einstein
I know that this question have been asked several times. But I can't get it to work.
我知道这个问题已经被问过好几次了。但我无法让它工作。
I installed Apache2 in my Ubuntu server I can also confirm that mod_rewrite is installed using phpinfo();. I have also put a file called .htaccess in my root folder(/var/www/.htaccess). In my .htaccess file I paste the following code:
我在我的 Ubuntu 服务器中安装了 Apache2 我还可以确认使用 phpinfo(); 安装了 mod_rewrite。我还在我的根文件夹(/var/www/.htaccess)中放置了一个名为 .htaccess 的文件。在我的 .htaccess 文件中,我粘贴了以下代码:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^.*$ test.php
So everything is redirected to test.php
所以一切都被重定向到 test.php
But it still doesn't work. So I checked my httpd.conf file under /etc/apache2. It is completely empty, with no lines of code (This seems a little odd to me)?! However checking in Stackoverflow answers there should be at least the following code in httpd.conf:
但它仍然不起作用。所以我检查了 /etc/apache2 下的 httpd.conf 文件。它完全是空的,没有代码行(这对我来说似乎有点奇怪)?!但是检查 Stackoverflow 的答案应该至少在 httpd.conf 中有以下代码:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>
So I paste that code in httpd.conf. And restarted Apache with sudo /etc/init.d/apache restart
. And it still does not work?
所以我将该代码粘贴到 httpd.conf 中。并使用sudo /etc/init.d/apache restart
. 它仍然不起作用?
I have also tested to open the file /sites-enabled/000-default and /sites-available/default, where all virtual host settings lies and change under the directory /var/www
and /
to AllowOverride All. The mod_rewrite still doesn't work. Can anyone please help me. This problem has been baking my nuts for a while.
我还测试打开文件/启用站点-/ 000-default和/站点可用/默认情况下,所有虚拟主机设置的谎言和改变的目录下/var/www
,并/
给了AllowOverride全部。mod_rewrite 仍然不起作用。谁能帮帮我吗。这个问题已经烤了我一段时间了。
Also, my apache2.conf file doesn't work. I tried to write som nonsense. And it is still gives me the normal result instead of http 500 error
另外,我的 apache2.conf 文件不起作用。我试着写些废话。它仍然给我正常的结果而不是 http 500 错误
采纳答案by Olivier Pons
All suggestions from Niels Bom are the smartest ones (I think). But I would add this as the first suggestion: try to launch and stop apache via the Ubuntu command: then when it's supposed to be stopped, make sureit's stopped ie verify your local page doesn't show anymore.
Niels Bom 的所有建议都是最聪明的(我认为)。但我会将此添加为第一个建议:尝试通过 Ubuntu 命令启动和停止 apache:然后当它应该停止时,请确保它已停止,即验证您的本地页面不再显示。
- read what's in the folder
/etc/apache2/
. There should beapache2.conf conf.d envvars httpd.conf mods-available mods-enabled ports.conf sites-available sites-enabled
- then if everything is ok, take a look at
mods-enabled
where you will find if the mods are enabled if so, then you can go on, otherwise take a look at the other answers about enabling modrewrite - take a look at
sites-enabled
: it's where you can find the sites that are... (guess what?) enabled. There should be thedefault
website. - if so, open this file, and try to put apache nonsense in it, restart the server and see if it's happy or not. If it's not happy that's good. If it's happy then you're working on the wrong file
- last: try to check what are the authorisations for htaccess filesfor this default vhost. Here's the way they work: if Apache doesn't find any directive in the vhost file, it will apply the global configuration. So I'd suggest to add the configuration in your vhost file, between the
<virtualhost>
-</virtualhost>
tags (of course).
- 阅读文件夹中的内容
/etc/apache2/
。应该有apache2.conf conf.d envvars httpd.conf mods-available mods-enabled ports.conf sites-available sites-enabled
- 那么如果一切正常,看看
mods-enabled
你会在哪里找到是否启用了 mods 如果是这样,那么你可以继续,否则看看关于启用 modrewrite 的其他答案 - 看一看
sites-enabled
:它是您可以找到...(猜猜是什么?)启用的站点的地方。应该有default
网站。 - 如果是这样,打开这个文件,试着把apache的废话放进去,重启服务器,看看它是否开心。如果它不开心,那很好。如果它很高兴,那么你正在处理错误的文件
- 最后:尝试检查此默认虚拟主机的htaccess 文件的授权。这是它们的工作方式:如果 Apache 在 vhost 文件中找不到任何指令,它将应用全局配置。所以我建议在你的 vhost 文件中添加配置,在
<virtualhost>
-</virtualhost>
标签之间(当然)。
Please tell me what's going on and I'd be glad to update my question accordingly.
请告诉我发生了什么,我很乐意相应地更新我的问题。
回答by bingjie2680
if you run this command,
如果你运行这个命令,
sudo a2enmod rewrite
ubuntu will output whethere it is activated or already running.
ubuntu 将输出它是否已激活或已在运行。
回答by Sliq
Because this thread ranks very high on google, here another solution:
因为这个线程在谷歌上排名很高,这里有另一个解决方案:
WHAT'S CAUSING THE PROBLEM ?
导致问题的原因是什么?
It's caused because the standard definition in apache simply blocks all .htaccess stuff. Seriouslsy. This seems to be the initial state in Ubuntu 12.04 LTS.
这是因为 apache 中的标准定义只是阻止了所有 .htaccess 的东西。认真的。这似乎是 Ubuntu 12.04 LTS 中的初始状态。
SOLUTION
解决方案
In /etc/apache2/sites-available/default (that's a file, not a folder) there's a code block like
在 /etc/apache2/sites-available/default (这是一个文件,而不是一个文件夹)有一个代码块,如
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride XXXXXXXXXXXXXXXXXXX
Order allow,deny
allow from all
</Directory>
Here you should change
在这里你应该改变
AllowOverride None
to
到
AllowOverride All
IMPORTANT
重要的
You have to do these changes with sudo rights, anotherwise Ubuntu will not save the file. After a RESTART your .htaccess stuff should work. To test .htaccess simply put some nonsense text into it and surf to localhost or 127.0.0.1 -> If you see "internal server error": voila! Remove the nonsense and you are ready to go.
您必须使用 sudo 权限进行这些更改,否则 Ubuntu 将不会保存该文件。重新启动后,您的 .htaccess 内容应该可以工作。要测试 .htaccess,只需将一些无意义的文本放入其中并浏览到 localhost 或 127.0.0.1 -> 如果您看到“内部服务器错误”:瞧!去掉废话,你就可以开始了。
回答by Niels Bom
Okay, let's check assumptions:
好的,让我们检查一下假设:
Put nonsense in your Apache config files (and restart Apache to let them take effect), does Apache "complain" on restart? Then it tries to load those files. Try this for every Apache config file you can find. You now have a complete list of Apache config files that are loaded.
put nonsense in your htaccess, reload the page, do you get a 500 error? If not, the htaccess is not getting loaded.
Put the most basic mod_rewrite statement in your htaccess:
Options +FollowSymLinks RewriteEngine On RewriteRule ^.*$ test.html
把废话放在你的 Apache 配置文件中(并重新启动 Apache 让它们生效),Apache 在重新启动时“抱怨”吗?然后它尝试加载这些文件。为您可以找到的每个 Apache 配置文件尝试此操作。您现在拥有已加载的 Apache 配置文件的完整列表。
把废话放在你的 htaccess 中,重新加载页面,你是否收到 500 错误?如果没有,htaccess 没有被加载。
将最基本的 mod_rewrite 语句放在您的 htaccess 中:
选项 +FollowSymLinks RewriteEngine On RewriteRule ^.*$ test.html
This should rewrite every request to test.html.
这应该重写对 test.html 的每个请求。
Try this and give us the results.
试试这个,然后给我们结果。
回答by Moshe Shaham
Please check /etc/apache2/apache2.conf
请检查 /etc/apache2/apache2.conf
回答by ThinkingMonkey
In your question, you have mentioned,
在你的问题中,你提到,
Also, my apache2.conf file doesn't work. I tried to write som nonsense. And it is still gives me the normal result instead of http 500 error
另外,我的 apache2.conf 文件不起作用。我试着写些废话。它仍然给我正常的结果而不是 http 500 错误
So, before posting more stuff to try-out with .conf files,
因此,在发布更多内容以试用 .conf 文件之前,
run strace -o somefilename.txt sudo /etc/init.d/apache restart
跑 strace -o somefilename.txt sudo /etc/init.d/apache restart
strace - trace system calls and signals : strace(1) - Linux man page.
strace - 跟踪系统调用和信号:strace(1) - Linux 手册页。
The -o somefilename.txt
part puts the output of strace
into somefilename.txt
. Open the text file and search for httpd.conf
. You should be able to see code similar to:
该-o somefilename.txt
部分将 的输出strace
放入somefilename.txt
. 打开文本文件并搜索httpd.conf
. 您应该能够看到类似于以下内容的代码:
stat("/etc/httpd/conf/httpd.conf", {st_mode=S_IFREG|0644, st_size=35894, ...})=0
open("/etc/httpd/conf/httpd.conf", O_RDONLY|O_CLOEXEC) = 3
The path to httpd.conf
will be replaced by the path to your default httpd.conf
路径httpd.conf
将替换为默认路径httpd.conf
If you find one, then open that httpd.conf
and make changes there. Else, a httpd.conf
is not getting loaded.
如果你找到一个,然后打开它httpd.conf
并在那里进行更改。否则, ahttpd.conf
没有被加载。
If later is the case, try:
如果以后是这种情况,请尝试:
sudo /etc/init.d/apache -f /path/to/httpd.conf start
Also, check for NameVirtualHost *:80
and make sure it is not commented.
另外,检查NameVirtualHost *:80
并确保它没有被评论。