使用. htaccess重定向标准化Web服务器地址

Answers to a couple of somewhat frequently-asked questions about the .htaccess tricks I’m using to standardize the domain names at Daring Fireball:

摆脱“www”前缀

I’ve always rather disliked the “www” prefix for web servers. It has become so common that everyone is accustomed to both seeing and saying it, and so it’s sort of invisible now, but it really is a goofy convention.1

很久以前,我开始使用. htaccessredirection to standardize all 万博manbetx贴吧 URLs on the prefix-less “www.gazogooz.com” address如果你试图加载"www.www.gazogooz.com/任何东西”, you’ll be redirected to “www.gazogooz.com/任何东西”。

Here’s the applicable portion of my .htaccess file, located in the root of my web hosting directory:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.daringfireball\.net$
RewriteRule (.*) http://www.gazogooz.com/$1 [R=Permanent]

第一行打开Apache的mod_rewrite模块The second line sets the “base” URL to the root of the domain.

The RewriteCond line specifies a condition for the subsequent rule — i.ethe rule on the next line is only applied if the condition in this line is metThere are two parts to the condition: a test string, and a regular expression to match against the test string. The parts are separated by spaces.

测试字符串。”% { HTTP_HOST }”, is a reference to the HTTP_HOST header that came in with the HTTP requestIt’s the name of the server the request has been sent toThe pattern, “^ www \ .daringfireball \美元。net”, is a precise regular expression that will successfully match when the HTTP_HOST header is “www.www.gazogooz.com”你可以写:

RewriteCond % { HTTP_HOST } www.www.gazogooz.com

which is less precise, but still matches “www.www.gazogooz.com”. But why be lazy? I’m not going to explain regular expression syntax here, so查一下if you don’t understand what’s going on in the precise version.

The RewriteRule line contains the rule that is applied when the preceding RewriteCond is metThe rule consists of three parts, again separated by spaces:

  • (. *)— a regular expression that is applied against the request stringThe request string is everything that comes after the slash after the domain nameSo if you request the URL “http://www.gazogooz.com/archives”, the request string that gets tested against this pattern is “archives”.

    这个特定的模式。”(. *)”,将匹配任何东西It even successfully matches “nothing” (a.k.athe empty string), which means it will successfully match when you request the URL “http://www.www.gazogooz.com/”包装它在圆括号中允许我们引用它之后,在替换字符串。

  • http://www.gazogooz.com/ 1美元——更换模式When the regular expression in the first part matches, this pattern describes how to rewrite the URL.1美元is a reference to the text captured by the sub-pattern within the parentheses; if we had a pattern with more than one set of parentheses, we could use2美元,3美元等to refer to the additional parenthesized sub-patterns.

    Because the pattern in the first part always matches, this replacement pattern is always applied.

  • (R =永久)— this is a flag that says that this redirection is permanentThat means the HTTP redirection goes out with a 301 code, which tells the client that the redirected URL is the new permanent home for the resource at the original URL你也可以写成(R = 301), but I prefer the wordy syntax.

    This flag is optional, but if you leave it off, the redirection will be marked as temporary — i.eit’ll go out with an HTTP 302 codeIf the redirection is permanent, it’s good practice to mark it soIntelligent client software will remember this and go directly to the new URL instead of going through the redirection on subsquent requests.

因此,在总结:

  1. 我们打开mod_rewrite处理。

  2. We use a RewriteCond condition that applies only when the HTTP_HOST variable matches “www.www.gazogooz.com”.

  3. When that condition is met, we apply a RewriteRule that takes the entire requestthe “www.www.gazogooz.com/” part, and redirects it to “http://www.gazogooz.com/”.

将从“。com”、“。net”

I can’t quite explain why I decided to use “www.gazogooz.com” instead of “daringfireball.com” as the canonical domain for Daring FireballSuffice it to say I have some sort of irrational dislike for “.com”.

But I realized early on that it was a somewhat common mistake for people to type “daringfireball.com” instead of “.net”, and the last thing I wanted was for some asshole domain squatter to register “daringfireball.com”, so I registered the “.com” version too.

But what to do with it? What I want is for any URL in the form “daringfireball.com/任何东西to redirect to “www.gazogooz.com/任何东西”听起来是不是很熟悉?

One solution to this problem would be to configure both domain names to point to the very same web site, much like how “www.example.com” and “example.com” almost always point to the same web site for any given domainI’m not doing this, however, and, uh, to be honest, I forget whyIf I had to guess, I’d say it’s because I had the vague notion that someday I might want to serve a different web site from .com than I do from .netAnyway, the point is that at my web hosting account, I have “daringfireball.com” configured as an entirely separate web site than “www.gazogooz.com”.

I have a simple one-line .htaccess file in the root directory for “daringfireball.com”:

RedirectMatch永久(. *)http://www.gazogooz.com 1美元

This pretty much does the same thing as the first example above, the one that gets rid of “www” prefixes, except that instead of using mod_rewrite, it uses the RedirectMatch command from Apache’s mod_alias module.

(There is absolutely no good reason that I couldn’t use mod_rewrite for this, toomod_rewrite is much more powerful than mod_alias’s RedirectMatch commandMy excuse is that I wrote this one first, and at the time, I didn’t know how to use mod_rewriteI use mod_rewrite for the “www” truncation because I have a bunch of other, more complicated redirect rules in my main .htaccess file, and those rules require mod_rewriteIt’s also worth noting that mod_alias is available on more web hosts than mod_rewrite is; but personally I’d never use a web host who didn’t enable mod_rewrite.)

The syntax for RedirectMatch should be self-explanatory after the previous explanation regarding RewriteRuleThe order of the components is different, however:

  • 永久— this specifies that the redirection is permanent and should use an HTTP 301 result code.

  • (. *)正则表达式匹配请求字符串。

  • http://www.gazogooz.com 1美元——替代模式。

The most interesting difference is that with RedirectMatch, the request string that the regular expression is tested against contains the leading “/”As someone who first learned to use RedirectMatch and then later switched to mod_rewrite, this has tripped me up dozens of times.

所以当使用mod_rewrite替代模式http://www.gazogooz.com/ 1美元, but the replacement pattern when using RedirectMatch ishttp://www.gazogooz.com 1美元, because with RedirectMatch, the text referenced by1美元already starts with a slash.

参考文献


  1. 我一直认为“web.example.com”会比“www.example.com”更好的公约人称之为“万维网”大约一个月前的1994年左右,我们都同意就称之为“网络”,但是我们坚持瘸子“www”前缀的我们的生活。↩︎

前一: 多孔径污垢
下一个: 目标