这个 .htaccess 文件有什么问题?
What's wrong with this .htaccess file?
我有一个部分工作的 .htaccess
文件,但我终究无法找出问题所在。
目标如下:我 example.com
我想成为其规范形式 www.example.com
。我有那个工作正常。我还有一个位于文件夹 /lang/chinese
中的子域,我想将其解析为 china.example.net
。这也很好用。最后我有(停放的)域 example.net
,我想将其重定向到 example.com
并因此解析为 www.example.com
.
这是最后一部分不起作用。如果我在浏览器中输入 www.example.net
,它会保留在地址栏中。
这是我的 .htaccess
文件的相关部分:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} china.example.com [NC]
RewriteRule ^(.*)$ http://china.example.com [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com [L,R=301]
RedirectMatch 301 ^/lang/chinese/(.*)$ http://china.example.com/
RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/ [R=permanent,nc,L]
显然我在这里做错了什么。我该如何解决这个问题?
对于此规则:
RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/ [R=permanent,nc,L]
^
告诉正则表达式匹配行的开头。所以这意味着您实际上不会在那里搜索“www”。
我相信你会想要将其更改为:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net [nc]
来自 Apache 的信息:
我有一个部分工作的 .htaccess
文件,但我终究无法找出问题所在。
目标如下:我 example.com
我想成为其规范形式 www.example.com
。我有那个工作正常。我还有一个位于文件夹 /lang/chinese
中的子域,我想将其解析为 china.example.net
。这也很好用。最后我有(停放的)域 example.net
,我想将其重定向到 example.com
并因此解析为 www.example.com
.
这是最后一部分不起作用。如果我在浏览器中输入 www.example.net
,它会保留在地址栏中。
这是我的 .htaccess
文件的相关部分:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} china.example.com [NC]
RewriteRule ^(.*)$ http://china.example.com [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com [L,R=301]
RedirectMatch 301 ^/lang/chinese/(.*)$ http://china.example.com/
RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/ [R=permanent,nc,L]
显然我在这里做错了什么。我该如何解决这个问题?
对于此规则:
RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/ [R=permanent,nc,L]
^
告诉正则表达式匹配行的开头。所以这意味着您实际上不会在那里搜索“www”。
我相信你会想要将其更改为:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net [nc]
来自 Apache 的信息: