Linux apache mod_rewrite 不适用于 .htaccess 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/17868819/
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
apache mod_rewrite not working with .htaccess file
提问by snuggles
OK, I've been having some issues with aws or something, such that I cannot seem to get mod_rewrite to work.
好的,我在使用 aws 或其他方面遇到了一些问题,以至于我似乎无法让 mod_rewrite 工作。
Just for testing purposes I've done the following:
仅出于测试目的,我做了以下工作:
1 used aws console to deploy fresh ami 64 bit instance from wizard
1 使用 aws 控制台从向导部署新的 ami 64 位实例
2 yum installed apache
2 yum 安装 apache
3 edited /etc/httpd/conf/httpd.conf: so that
3 编辑 /etc/httpd/conf/httpd.conf: 这样
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
looks like
好像
<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>
4 made sure that
4 确保
LoadModule rewrite_module modules/mod_rewrite.so
is in the file and uncommented.
位于文件中且未注释。
5 restarted apache:
5 重启apache:
 sudo /sbin/service httpd restart
6 created two new files:
6 创建了两个新文件:
/var/www/html/test1.html
contains:
包含:
this is test1!
/var/www/html/test2.html
contains:
包含:
this is test2!
7 created file:
7 创建的文件:
/var/www/html/.htaccess
contains (IN TOTAL):
包含(总计):
RewriteEngine on
RewriteRule ^test1\.html$ test2.html [L]
8 went to:
8 去了:
http://[my aws server]/test1.html
Am getting "this is test1!"
我得到“这是 test1!”
I am doing something wrong here, but for the life of me I have no idea what. Any help is greatly appreciated...
我在这里做错了什么,但对于我的生活,我不知道是什么。任何帮助是极大的赞赏...
EDIT: I added nonsense chars/numbers to the beginning of my .htaccess file, and restarted apache (not 100% sure that is needed, but what the hey...), and nothing happened. In other words, I expected that going to the url [aws server]/test1.html would result in some kind of error, but it did not. I suspect apache is not even reading the .htaccess file.
编辑:我在我的 .htaccess 文件的开头添加了无意义的字符/数字,并重新启动了 apache(不是 100% 确定需要,但是嘿......),什么也没发生。换句话说,我预计访问 url [aws server]/test1.html 会导致某种错误,但事实并非如此。我怀疑 apache 甚至没有读取 .htaccess 文件。
EDIT: I added the following to my httpd.conf file:
编辑:我在 httpd.conf 文件中添加了以下内容:
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
The file is created when I restart apache, but nothing ever goes in there when I go to either page I've set up. I'm failing to do something very, very basic here, but I'm not sure what...
该文件是在我重新启动 apache 时创建的,但是当我转到我设置的任一页面时,那里没有任何内容。我没有在这里做一些非常非常基本的事情,但我不确定是什么......
采纳答案by Jon Lin
Not sure if this is the cause of your problems, but you shouldn't mess with the
不确定这是否是您的问题的原因,但您不应该弄乱
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
line, and it should be something like:
行,它应该是这样的:
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Deny from all
</Directory>
You should add the directory of your document root as a different container:
您应该将文档根目录添加为不同的容器:
<Directory /var/www/html/>
    Options FollowSymLinks
    AllowOverride All
    Allow from all
</Directory>
回答by Alex Rapso
Took me a while to find this but in some installs Apache will use multiple config files.
我花了一段时间才找到它,但在某些安装中,Apache 将使用多个配置文件。
Look in "/etc/apache2/sites-enabled/000-default" and check that AllowOverideis set to All
查看“ /etc/apache2/sites-enabled/000-default”并检查AllowOveride是否设置为All
回答by Duy Nguyen
Try it. This work for me. The first, you need to make sure the .htaccess file put in correct directory. For this, you go to sites-enabled folder and check which the .conf files are enabled.
尝试一下。这对我有用。首先,您需要确保 .htaccess 文件放在正确的目录中。为此,您转到启用站点的文件夹并检查启用了哪些 .conf 文件。
cd /etc/apache2/sites-enabled
ls
Ex: 000-default.conf
例如:000-default.conf
Then, goto sites-available folder to edit that .conf file.
然后,转到sites-available 文件夹以编辑该.conf 文件。
cd ../sites-available
sudo gedit 000-default.conf
Find to DocumentRoot and check directory again. If you put .htaccess file in /var/www/html/.htaccess so this line look like this:
找到 DocumentRoot 并再次检查目录。如果将 .htaccess 文件放在 /var/www/html/.htaccess 中,则此行如下所示:
DocumentRoot /var/www/html/
The second, You need modify <Directory>block look like this.
第二,您需要<Directory>像这样修改块。
    <Directory /var/www/html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
    # This directive allows us to have apache2's default start page
            # in /apache2-default/, but still have / go to the right place
            #RedirectMatch ^/$ /apache2-default/
    </Directory>
Finally, you save file and restart apache
最后,您保存文件并重新启动 apache
service apache2 restart
Hope this help!
希望这有帮助!

