我的網址由我做主 non-www or www

Author: Eric  //  Category: Free Teaching

00. 如果您想一統天下, 讓 non-www url 在網址前面都加上 www 或您想把 www url 的 www 都去掉。因為搜尋引擎會視 non-www 和 www 為兩個不同的網站, 為了 SEO 著想, 或者是想讓搜尋引擎, 固定都收錄 non-www or www 的網址, 而不會三心二意, 一下收錄 non-www 一下收錄 www。

01. Redirect Non-WWW to WWW
// eg: Redirect who-know.com to www.who-know.com, 於 .htaccess file 加上

# 301 Redirect Non-WWW to WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(who-know\.com)(:80)? [NC]
RewriteRule ^(.*) http://www.who-know.com/$1 [R=301,L]

02. Redirect WWW to Non-WWW
// eg: Redirect www.who-know.com to who-know.com, 於 .htaccess file 加上

# 301 Redirect WWW to Non-WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.who-know\.com)(:80)? [NC]
RewriteRule ^(.*) http://who-know.com/$1 [R=301,L]

03. 如果覺得自己改有點麻煩, 可以利用線上製作 .htaccess file ( ) 來產生, 點左邊的設置WWW(w) -> 然後填上您希望統一的網址 -> 複製貼上到您的 .htaccess file
// 參考上面舉的例子, 複製須要的部分即可, 用不到的就不用管它。

<Files ~ "^.(htaccess|htpasswd)$">
deny from all
</Files>
order deny,allow

04. 上傳完 .htaccess file, 可以利用 HTTP Server Header Checker, SEO Tools - Search Engine Friendly Redirect Checker 來檢查是否設定成功。

05. 關於上面 Rewrite Rule 所用到的 NC, $1, R, L 的解釋

  a. [NC] (no case) Case-insensitive, ignoring difference between 'A-Z' and 'a-z' when Pattern is matched against the current URL.

  b. $1 is magically replaced with the text which was captured previously. ( $ stuff means plug in whatever follows )

  c. L - Last Rule. No more rules will be proccessed if this one was successful.

  d. R[=code] - Redirect. The user's web browser will be visibly redirected to the substituted URL. If you use this flag, you must prefix the substitution with http://www.somesite.com/, thus making it into a true URL. If no code is given, then a HTTP reponse of 302 (temporarily moved) is sent.

06. 另外, 大家常說的 301、302, 實際上是 301、307?

  a. 301 Moved Permanently

The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. This response is cacheable unless indicated otherwise.

The new permanent URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

  b. 302 Found

The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

  c. 307 Temporary Redirect

The requested resource resides temporarily under a different URI. Since the redirection MAY be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s) , since many pre-HTTP/1.1 user agents do not understand the 307 status. Therefore, the note SHOULD contain the information necessary for a user to repeat the original request on the new URI.

If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

08. 由於實在太好奇 302 v.s. 307, 於是又 Google 了一下, 終於發現原因了, 原來一個是 HTTP/1.0 Hypertext Transfer Protocol (下面), 一個是 HTTP/1.1 Hypertext Transfer Protocol (上面)。

  a. 301 Moved Permanently

The requested resource has been assigned a new permanent URL and any future references to this resource should be done using that URL. Clients with link editing capabilities should automatically relink references to the Request-URI to the new reference returned by the server, where possible.
The new URL must be given by the Location field in the response. Unless it was a HEAD request, the Entity-Body of the response should contain a short note with a hyperlink to the new URL.

If the 301 status code is received in response to a request using the POST method, the user agent must not automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

  b. 302 Moved Temporarily

The requested resource resides temporarily under a different URL. Since the redirection may be altered on occasion, the client should continue to use the Request-URI for future requests.
The URL must be given by the Location field in the response. Unless it was a HEAD request, the Entity-Body of the response should contain a short note with a hyperlink to the new URI(s).

If the 302 status code is received in response to a request using the POST method, the user agent must not automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

Ref: // 其實只有參考第一個, 複製了它的解釋, 其它篇是覺得寫得不錯, 值得收藏一下^^
01. mod_rewrite, a beginner's guide (with examples)
02. 301-redirect
03. Apache mod_rewrite
04. SEO Tips - Mod Rewrite Non-WWW to WWW
05. Using Mod-rewrite to Redirect Non-www URL to www URL
06. Improve Your SEO By Redirecting Non-WWW Traffic to WWW
07. Redirect Visitors From Non-WWW To WWW Version Of Your Website With .htaccess And mod_rewrite

Tags: , ,

One Response to “我的網址由我做主 non-www or www”

  1. 万戈 Says:

    在GG的网站管理员工具中可以设置的,不过可能只对GG有效

Leave a Reply