简体   繁体   中英

.htaccess force www. and Silex

I want to force 'www' prefix on my website. I know there is all about that on net, but i'm using Silex and the trick is to make it work together.

Here is my .htaccess file:

<IfModule mod_rewrite.c>
   RewriteEngine On
   ErrorDocument 404 /404.html
   RewriteRule ^$ index.php?/ [QSA,L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   #RewriteCond %{HTTP_HOST} !^$
   #RewriteCond %{HTTP_HOST} !^www\. [NC]
   #RewriteCond %{HTTPS}s ^on(s)|
   RewriteRule (.*) index.php?/$1 [QSA,L]
   #RewriteRule (.*) http%1://www.%{HTTP_HOST}%/index.php?/$1 [R=301,L]
   #RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

You have a bunch of conditions just jumbled in your htaccess file what looks like random order. The RewriteCond only works for the first RewriteRule following them.

If you put this

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

at the top of your htaccess after RewriteEngine On and before your other rules it should handle your www issue

I also would put ErrorDocument 404 /404.html at the bottom of your rewrite rules as it's not a rewrite and doesn't need to be in the rules block confusing things.

</IfModule>
ErrorDocument 404 /404.html

If this works for you let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM