【问题标题】:Redirect non www domain to a www domain without affecting subdomains将非 www 域重定向到 www 域而不影响子域
【发布时间】:2015-10-16 18:14:08
【问题描述】:

@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>

请帮忙

【问题讨论】:

  • @Starkeen,这就是我所拥有的。好像有点长吧?
  • @Starkeen 保存了它,但没有添加 www,所有其他页面都出现 404 错误,帮助伙伴!非常感谢

标签: .htaccess cpanel


【解决方案1】:

在“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>

【讨论】:

  • 谢谢@Starkeen,我需要在您的代码和该指令之间留一个空格吗?我也应该从底部删除 RewriteCond/RewriteRule 吗?
  • 首页在不加www的情况下正常工作,其他页面全部下线
  • Zwan ,抱歉有错字,我编辑了我的答案,重新加载页面以查看它。
  • 主页在线但仍然没有 www 并且所有其他页面都收到 404 错误。这是该网站的链接:bit.ly/1CZqrN2
  • 清空浏览器缓存试试
猜你喜欢
  • 1970-01-01
  • 2014-03-25
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 2011-04-01
相关资源
最近更新 更多