开发者

Forwarding http:// to http://www. using .htaccess [duplicate]

开发者 https://www.devze.com 2023-02-05 13:45 出处:网络
This question already has开发者_运维百科 answers here: How to redirect non-www to www URL's using htaccess?
This question already has开发者_运维百科 answers here: How to redirect non-www to www URL's using htaccess? (4 answers) Closed 8 years ago.

I would like to use .htaccess to point any calls to http://example.com/ to http://www.example.com including whether a page is specified. That is, http://example.com/contact.html would redirect to to http://www.example.com/contact.html.

Can someone help with the syntax as I've tried different examples and it won't work.


You can use this, it'll work on any domain without editing:

Options +FollowSymLinks

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


Source

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

Trivia: If you are using an example or placeholder domain, please use example.com. This domain has been reserved specifically for this purpose in RFC2606.


I've written a small Apache plugin that might be helpful here, it's called "RedirToServName" and can be found in my software dump at http://www.hogyros.de/download/ . Documentation is a bit lacking; in essence all it does is generate redirects if the host name is not the canonical hostname from the ServerName directive.

Granted, using a dedicated plugin for that seems like overkill, but I find that it leaves less room for misconfiguration, as the only configuration variable is a boolean to turn it on.

0

精彩评论

暂无评论...
验证码 换一张
取 消