使用.htaccess将非www重定向到www

时间:2014-12-09 06:34:42

标签: php apache http yii

这是我的.htacess:

Options +FollowSymLinks 
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

我想将非www请求重定向到www.example.com,但是当我添加

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

我的css和js以及其他文件现在出现404错误,所有文件请求都转到yii控制器

框架:yii2

PHP 5.4

Apache:2.4.7

1 个答案:

答案 0 :(得分:2)

我找到了这个答案:

RewriteEngine on

#non-www to www
RewriteCond %{HTTP_HOST} ^adnasor\.ir 
RewriteRule (.*) http://www.adnasor.ir/$1 [R=301,L]

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php