Symfony2 - Redirect from non-www to www

只愿长相守 提交于 2019-12-08 04:10:55

问题


I need to redirect my symfony website from "abc.com" to "www.abc.com". I have tried some solutions in stack overflow but they add multiple "www." to the address and others didn't work.

Here is my htaccess source.

DirectoryIndex app.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /app.php/
    </IfModule>
</IfModule>

How can I do this?


回答1:


Add the following rule as the first rule:

RewriteCond %{HTTP_HOST} !^(www)\.
RewriteRule ^(.*?)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


来源:https://stackoverflow.com/questions/43862282/symfony2-redirect-from-non-www-to-www

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