rewrite_mod is enabled but .htaccess is not working

ぐ巨炮叔叔 提交于 2019-12-24 01:42:54

问题


I am using apache 2.2 in ubuntu 12.04 in Amazon EC2. I enabled mod_rewrite using

sudo a2enmod rewrite

and able to see with

apache2ctl -M

now I wrote the .htaccess code as following and is not working

# Redirect everything in this directory to "good.html"



    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule .* good.html

Here is my contents of /etc/apache2/sites-available/default http://pastebin.com/urNfmqan

when I add LoadModule rewrite_module modules/mod_rewrite.so to the httpd.conf I get this waiting [Thu Apr 11 18:44:12 2013] [warn] module rewrite_module is already loaded, skipping and htaccess is not working even then ( I presume in apache2.2 we don't edit httpd.conf to enable rewrite_mod but as per anubhava comments I modified the question)


回答1:


Try this:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /good.html [QSA,L]
</IfModule>

You should change the Base and the second element in the rewrite rule to whatever your directory is if it's not root.




回答2:


Looks like you will have a rewrite loop. Apache rewrite doesn't run the rules once, but once for every time a path is requested. So on first load it rewrites .* to good.html. Then it makes an internal request for good.html and re-processes the rules again, rewriting again and makes another internal request to infinity. Revent's answer tries to solve this by checking if the file exists. You get index.html in his rules because index.html exists. If you removed index.html the file wouldn't exist, the rewrite condition would match and the rule would rewrite to good.html. When re-processing the rules, the file exists, the condition fails and redirect stops after that.

To fix, you can add the [L] flag after your rewrite rule to tell apache to stop processing rules after this one runs which should stop the loop. If that doesn't help, you can add a rewriteCond to check that the request_filename is not good.html causing the rule to fail after redirect.



来源:https://stackoverflow.com/questions/15956131/rewrite-mod-is-enabled-but-htaccess-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!