我的.htaccess文件中有这个:

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

但每当我访问根目录下的文件,比如http://example.com/robots.txt,它就会重定向到http://www.example.comrobots.txt/。

我如何纠正这一点,使它将正确重定向到http://www.example.com/robots.txt?


当前回答

以下是支持https和http的正确解决方案:

# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

乌利希期刊指南。:用于像.co这样的域名。英国,取代

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$

with

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+\.[^.]+$

其他回答

试试这个,我在很多网站上都用过,效果很好

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^bewebdeveloper.com$
RewriteRule ^(.*) http://www.bewebdeveloper.com/$1  [QSA,L,R=301]

写入。htaccess:)

## Redirect from non-www to www (remove the two lines below to enable)
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

在.htaccess文件中添加以下代码。

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

url重定向教程可以从这里找到-重定向非www到www & HTTP到HTTPS使用。htaccess文件

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.

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

为Https

RewriteCond %{HTTPS}s ^on(s)|

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

我已经测试了上面所有的解决方案,但不适合我,我已经尝试删除http://,不会重定向也删除了www它重定向好,所以我感到困惑,特别是我在https://下运行我所有的网站

所以我已经结合了一些代码在一起,并提出了完美的解决方案,无论是http://和https://和www和非www。

# HTTPS forced
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# Redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

希望这能帮助到一些人:)