我的.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} ^[^.]+\.[^.]+\.[^.]+$

其他回答

以下是支持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} ^[^.]+\.[^.]+\.[^.]+$
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]

这将重定向您的域名,不是以WWW开始的 它不是重定向你的所有子域。

它很有用。

这个配置为我在bitnami wordpress与SSL配置工作:

在/opt/bitnami/apps/wordpress/conf/httpd-app.conf文件中的“RewriteEngine On”下增加了以下内容

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

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

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

写入。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]