.htaccess 银条通道

.htaccess 银条通道,.htaccess,silverstripe,.htaccess,Silverstripe,我有一个Silverstripe平台网站,它有www、非www、http和https的重复URL 似乎有多种解决方案,但没有明确的答案。是否有人知道Silverstripe的htaccess文件的正确代码 我想得到所有指向https://www的页面 这是htaccess文件中的当前代码- ErrorDocument 401 /base/401.txt ### SILVERSTRIPE START ### # Deny access to templates (but allow f

我有一个Silverstripe平台网站,它有www、非www、http和https的重复URL

似乎有多种解决方案,但没有明确的答案。是否有人知道Silverstripe的htaccess文件的正确代码

我想得到所有指向https://www的页面

这是htaccess文件中的当前代码-

    ErrorDocument 401 /base/401.txt

### SILVERSTRIPE START ###

# Deny access to templates (but allow from localhost)
<Files *.ss>
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Files>

# Deny access to IIS configuration
<Files web.config>
    Order deny,allow
    Deny from all
</Files>

# Deny access to YAML configuration files which might include sensitive information
<Files ~ "\.ya?ml$">
    Order allow,deny
    Deny from all
</Files>

# Route errors to static pages automatically generated by SilverStripe
ErrorDocument 404 /assets/error-404.html
ErrorDocument 500 /assets/error-500.html

<IfModule mod_env.c>
    # Ensure that X-Forwarded-Host is only allowed to determine the request
    # hostname for servers ips defined by SS_TRUSTED_PROXY_IPS in your _ss_environment.php
    # Note that in a future release this setting will be always on.
    SetEnv BlockUntrustedIPs true
</IfModule>

<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
    </IfModule>

    RewriteEngine On
    
    # non-www to www redirect
    #RewriteCond %{HTTP_HOST} ^bolstered.com.au$ [NC]
    #RewriteRule (.*) https://www.bolstered.com.au/$1 [R=301,L]
    
    # http to https redirect
    #RewriteCond %{HTTPS} !=on 
    #RewriteRule ^ (.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    # Enable HTTP Basic authentication workaround for PHP running in CGI mode
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    # Deny access to potentially sensitive files and folders
    RewriteRule ^vendor(/|$) - [F,L,NC]
    RewriteRule silverstripe-cache(/|$) - [F,L,NC]
    RewriteRule composer\.(json|lock) - [F,L,NC]
    
    RewriteCond %{REQUEST_URI} ^(.*)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !\.php$
    RewriteRule .* index.php?url=%1&%{QUERY_STRING} [L]
</IfModule>
### SILVERSTRIPE END
ErrorDocument 401/base/401.txt
###银条起点###
#拒绝访问模板(但允许从本地主机访问)
命令拒绝,允许
全盘否定
允许从127.0.0.1开始
#拒绝访问IIS配置
命令拒绝,允许
全盘否定
#拒绝访问可能包含敏感信息的YAML配置文件
命令允许,拒绝
全盘否定
#将错误路由到SilverStripe自动生成的静态页面
ErrorDocument 404/assets/error-404.html
ErrorDocument 500/assets/error-500.html
#确保只允许X-Forwarded-Host确定请求
#由您的_SS_environment.php中的SS_TRUSTED_PROXY_IP定义的服务器IP的主机名
#请注意,在将来的版本中,此设置将始终处于启用状态。
SetEnv blockuntrustdips true
#关闭index.php处理apache>=2.4中主页修复问题的请求
目录索引已禁用
重新启动发动机
#非www到www重定向
#RewriteCond%{HTTP_HOST}^boolsted.com.au$[NC]
#重写规则(.*)https://www.bolstered.com.au/$1[R=301,L]
#http到https重定向
#重写cond%{HTTPS}=在…上
#重写规则^(.*)$https://%{HTTP_HOST}%{REQUEST_URI}[R=301,L]
#为在CGI模式下运行的PHP启用HTTP基本身份验证解决方案
重写规则。*-[E=HTTP\U授权:%{HTTP:AUTHORIZATION}]
#拒绝访问可能敏感的文件和文件夹
重写规则^供应商(/|$)-[F、L、NC]
重写规则silverstripe缓存(/|$)-[F,L,NC]
重写规则生成器\(json锁)-[F,L,NC]
RewriteCond%{REQUEST_URI}^(.*$
重写cond%{REQUEST_FILENAME}-F
重写条件%{REQUEST\u URI}!\。php$
重写规则。*index.php?url=%1&%{QUERY\u STRING}[L]
###银条端

我通常会将SilverStripe部分保持在
###SilverStripe START####
###SilverStripe END###
之间,并将我的规则放在SilverStripe部分之前或之后

重写引擎打开两次没有问题。
我也没有费心检查mod_rewrite是否存在,因为我所有的服务器都将启用它,我不会让客户机将它放在没有它的服务器上

下面是我通常在项目中使用的
.htaccess
的完整示例:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteCond %{HTTP_HOST} !^127.0.0.1 [NC]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.examle\.org [NC]
RewriteRule ^ https://www.examle.org%{REQUEST_URI} [R=301,L]


### SILVERSTRIPE START ###

# Deny access to templates (but allow from localhost)
<Files *.ss>
    Require ip 127.0.0.1
</Files>

# Deny access to IIS configuration
<Files web.config>
    Require all denied
</Files>

# Deny access to YAML configuration files which might include sensitive information
<Files ~ "\.ya?ml$">
    Require all denied
</Files>

# Route errors to static pages automatically generated by SilverStripe
ErrorDocument 404 /assets/error-404.html
ErrorDocument 500 /assets/error-500.html

<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

    # Enable HTTP Basic authentication workaround for PHP running in CGI mode
    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]
    RewriteRule ^Security - [F,L,NC]

    # Process through SilverStripe if no file with the requested name exists.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index.php
</IfModule>
### SILVERSTRIPE END ###
  • 4
    RewriteCond
    是与其下方的
    RewriteRule
    相关的所有条件

    RewriteCond %{HTTP_HOST} !^localhost [NC] 
    RewriteCond %{HTTP_HOST} !^127.0.0.1 [NC]
    RewriteCond %{HTTPS} !on [OR]
    RewriteCond %{HTTP_HOST} !^www\.examle\.org [NC]
    
    除非您添加
    [或]
    ,否则多个条件将是逻辑的

    [NC]
    表示不区分大小写,因此不区分大小写

    前两个是localhost/127.0.0.1的例外,以确保在工作站上开发时不会执行重定向。
    3正在检查https是否关闭
    4正在检查域是否正确

    所以用伪代码来说,它是这样的:

    if($HTTP\u HOST!=“localhost”&&$HTTP\u HOST!=“127.0.0.1”和($HTTPS!=“on”或$HTTP\u HOST!=“www.examle.org”){
    不要重定向();
    }
    
  • 实际重定向

    RewriteRule ^ https://www.examle.org%{REQUEST_URI} [R=301,L]
    
    它正在重定向到所需的域,并将路径(
    /foo/bar
    )和查询参数(
    ?foo=bar
    )附加到该域。
    R=301是http响应代码。如果需要临时重定向,可以将其设置为302。
    L
    意味着
    Last
    我认为,这将在此时停止处理,并且不会继续尝试下面的其他规则


  • 备选方案:

    .htaccess是最好的方法。但值得指出的是,这不是唯一的选择

    您可以在普通PHP中,在任何/大多数Web服务器的配置中。。。 SilverStripe具有用于执行检查和重定向的内置方法:

    Director::forceSSL();
    Director::forceWWW();
    

    但是如前所述,.htaccess要好得多(速度更快,只需一次重定向)

    非常感谢您的建议和解释。您能告诉我网站是否为www.boolsted.com.au吗?在您提供的代码中,我在哪里添加或替换此网址?我替换并添加了您的整个htaccess代码-RewriteCond%{HTTP_HOST}!^www.boolsted.com.au[NC]RewriteRule^{REQUEST_URI}[R=301,L]这破坏了网站当你说“破坏了网站”时,那是什么意思?无休止的重定向?如果是无休止的重定向,那么可能是因为你忘了添加“www”对于RewriteRule。非常感谢您的帮助。非常感谢。问题已经解决。当使用像您这样的自定义站点时,您通常需要使用自定义htaccess规则来强制重定向到www和https。RewriteEngine on RewriteCond%{HTTP_HOST}^boostered\.com\.au[NC]RewriteRule^(.*)[L,R=301]强制站点在RewriteCond%{HTTPS}off RewriteRule^(.*)$HTTPS://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]上使用www子域重写引擎强制站点使用HTTPS
    Director::forceSSL();
    Director::forceWWW();