.htaccess not working (mod_rewrite)

前端 未结 16 1769
遇见更好的自我
遇见更好的自我 2020-12-08 05:05

I have not having any luck getting my .htaccess with mod_rewrite working. Basically all I am trying to do is remove \'www\' from \"http://www.example.com\" and \"https://ww

16条回答
  •  -上瘾入骨i
    2020-12-08 05:29

    First of all, check to make sure that mod_rewrite is actually loading. You can do that with apache2ctl:

    [root@host ~]# apache2ctl -t -D DUMP_MODULES 2>&1 |grep rewrite
    rewrite_module (shared)
    

    If it is not, then you might have to run 'a2enmod rewrite'

    Next, test to see if your .htaccess file is even being read. I typically do this by putting some garbage inside the .htaccess file, then loading a page in that directory in my browser and verifying that I get a 500 error

    On a side note, as others have mentioned, if you have the ability to modify your Apache config directly, you should put the Rewrite Rules there instead of in the .htaccess file as it is less efficient. Apache has to first decide in which directory to look for the .htaccess file, then read it, then perform the rewrites. If the RewriteRules are specified inside your VirtualHost directives, then it can do them before finding the .htaccess file. Specifying them inside your VirtualHost also means that it doesn't matter if your .htaccess file is being read. It would look something like this:

     
        .... existing config ....
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
        RewriteRule (.*) http://%1/$1 [L,R=301]
     
     
        .... existing config ....
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
        RewriteRule (.*) https://%1/$1 [L,R=301]
     
    

提交回复
热议问题