2
A
回答
1
3dgoo的解決方案是更快,因爲它不涉及任何PHP的,但我沒有......
class PreventWWW extends DataExtension {
public static function stopWWW() {
if(!(strpos($_SERVER['HTTP_HOST'], 'www') !== 0)) {
$destURL = str_replace(Director::protocol() .'www.', Director::protocol() ,
Director::absoluteURL($_SERVER['REQUEST_URI']));
header("Location: $destURL", true, 301);
die("<h1>Your browser is not accepting header redirects</h1>"
. "<p>Please <a href=\"$destURL\">click here</a>");
}
}
}
in yml config:
Director:
extensions:
- PreventWWW
既然我將它只是活ENV我_config.php
PreventWWW::stopWWW();
有這個也應該是可以設置它依賴於陽明海運環境(活的/ dev /臺),每「只」。看到這裏.. http://doc.silverstripe.org/framework/en/topics/configuration
2
沒有SilverStripe函數可將所有www鏈接重定向到非www鏈接。
相反,你可以寫一個.htaccess
RewriteRule
來做到這一點。
在您的網站根.htaccess
文件中添加以下代碼根據現有RewriteEngine On
行:
...
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
### Redirect www links to non www links ###
RewriteCond %{HTTP_HOST} ^www\.domainName\.com$ [NC]
RewriteRule (.*) http://domainName.com/$1 [R=301,L]
...
如果你使用apache,你應該使用3dgoo的解決方案,因爲它的速度更快。如果您選擇使用此解決方案,請注意這個解決方案:如果您在命令行上運行它,它會中斷,因此它應該更新以檢查(Director :: is_cli())。另外,您不需要添加yaml擴展或擴展DataExtension,因爲這只是一個靜態方法 - Director從不實例化,因此擴展永遠不會被應用。 – micmania1 2016-01-15 00:12:55
@ micmania1你是對的:)所有關於速度我也在最初的回答中說明了這一點,第二個 - outch :(至少有人注意到 - 所以我會更新。 – munomono 2016-02-15 21:10:29