使用最新版本的 SilverStripe,他们鼓励您使用服务器端规则进行 URL 重写,而不是Director::forceSSL();
和/或Director::forceWWW();
在您的_config.php
文件中使用,因为它被认为是不可靠的。
在 Apache 服务器上,这在逻辑上似乎表明它应该通过.htaccess
文件进行管理。不幸的是,下面显示的片段可以独立触发重写,但是在单个文件中链接或组合似乎跳过了 www 或 https 情况。
### SILVERSTRIPE START ###
### TRIMMED ROBOT/ERROR CODE ###
<IfModule mod_rewrite.c>
# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4
<IfModule mod_dir.c>
DirectoryIndex disabled
DirectorySlash On
</IfModule>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
RewriteBase '/'
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Deny access to potentially sensitive files and folders
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule ^\.env - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]
RewriteRule (error|silverstripe|debug)\.log - [F,L,NC]
# Process through SilverStripe if no file with the requested name exists.
# Pass through the original path as a query parameter, and retain the existing parameters.
# Try finding framework in the vendor folder first
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
</IfModule>
### SILVERSTRIPE END ###
<IfModule mod_rewrite.c>
### FORCE TRAILING SLASH ###
### Source - https://paulund.co.uk/using-htaccess-to-force-trailing-slash ###
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
### FORCE WWW ###
#### Modified from source https://paulund.co.uk/add-www-subdomain-to-all-urls-using-htaccess ###
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
### FORCE SSL ###
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com/$1 [R=301,L]
</IfModule>