rewrite_mod is enabled but .htaccess is not workin

2019-07-18 01:11发布

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)

2条回答
Bombasti
2楼-- · 2019-07-18 01:41

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.

查看更多
看我几分像从前
3楼-- · 2019-07-18 02:07

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.

查看更多
登录 后发表回答