将非www域重定向到www域而不影响子域

时间:2015-07-26 14:36:25

标签: .htaccess cpanel

@Starkeen,这段代码对我不起作用。所有其他页面现在有404错误,主页没有添加www。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sitename.com$
RewriteRule ^(.*)$ http://www.sitename.com/$1 [NC,R=301,L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [R=301,L]
</IfModule>

请帮助

1 个答案:

答案 0 :(得分:0)

在“RewriteEngine on”指令

之后立即将其添加到您的htaccess文件中
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [NC,R=301]

修改

在首次重写

中添加了缺少的 L 标记

试试这个:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [NC,R=301,L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [NC,R=301,L]
</IfModule>