CanonicalURLMiddleware
class CanonicalURLMiddleware implements HTTPMiddleware (View source)
Implements the following URL normalisation rules
- redirect basic auth requests to HTTPS
- force WWW, redirect to the subdomain "www."
- force SSL, redirect to https
- force the correct path (with vs without trailing slash)
Traits
A class that can be instantiated or replaced via DI
Provides extensions to this object to integrate it with standard config API methods.
Properties
protected | bool | $enforceTrailingSlashConfig | If set, the trailing slash configuration set in Controller::add_trailing_slash is enforced with a redirect. |
|
protected | array | $enforceTrailingSlashConfigIgnorePaths | If enforceTrailingSlashConfig is enabled, this is the list of paths that are ignored |
|
protected | array | $enforceTrailingSlashConfigIgnoreUserAgents | If enforceTrailingSlashConfig is enabled, this is the list of user agents that are ignored |
|
protected | bool | $forceWWW | Set if we should redirect to WWW |
|
protected | bool | $forceSSL | Set if we should force SSL |
|
protected | bool|null | $forceBasicAuthToSSL | Set if we should automatically redirect basic auth requests to HTTPS. A null value (default) will cause this property to return the value of the forceSSL property. |
|
protected | int | $redirectType | Redirect type |
|
protected | array|bool | $enabledEnvs | Environment variables this middleware is enabled in, or a fixed boolean flag to
apply to all environments. cli is disabled unless present here as |
|
protected | array | $forceSSLPatterns | If forceSSL is enabled, this is the list of patterns that the url must match (at least one) |
|
protected | string | $forceSSLDomain | SSL Domain to use |
Methods
An implementation of the factory method, allows you to create an instance of a class
Creates a class instance by the "singleton" design pattern.
Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).
Gets the uninherited value for the given config option
No description
Handles redirection to canonical urls outside of the main middleware chain using HTTPResponseException.
Return a valid request, if one is available, or null if none is available
Check if a redirect for trailing slash is necessary
Set enabled flag, or list of environments to enable in.
Determine whether the executed middlewares have added a basic authentication prompt
Redirect the current URL to the specified HTTP scheme
Details
static Injectable
create(mixed ...$args)
An implementation of the factory method, allows you to create an instance of a class
This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.
This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create(SiteTree::class); $list = SiteTree::get();
static Injectable
singleton(string $class = null)
Creates a class instance by the "singleton" design pattern.
It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).
static Config_ForClass
config()
Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).
mixed
uninherited(string $name)
Gets the uninherited value for the given config option
CanonicalURLMiddleware
setEnforceTrailingSlashConfig(bool $value)
No description
bool
getEnforceTrailingSlashConfig()
No description
CanonicalURLMiddleware
setEnforceTrailingSlashConfigIgnorePaths(array $value)
No description
array
getEnforceTrailingSlashConfigIgnorePaths()
No description
CanonicalURLMiddleware
setEnforceTrailingSlashConfigIgnoreUserAgents(array $value)
No description
array
getEnforceTrailingSlashConfigIgnoreUserAgents()
No description
array
getForceSSLPatterns()
No description
$this
setForceSSLPatterns(array $forceSSLPatterns)
No description
string
getForceSSLDomain()
No description
$this
setForceSSLDomain(string $forceSSLDomain)
No description
bool
getForceWWW()
No description
$this
setForceWWW(bool $forceWWW)
No description
bool
getForceSSL()
No description
$this
setForceSSL(bool $forceSSL)
No description
$this
setForceBasicAuthToSSL(bool|null $forceBasicAuth)
No description
bool
getForceBasicAuthToSSL()
No description
HTTPResponse
process(HTTPRequest $request, callable $delegate)
Generate response for the given request
protected HTTPResponse|null
getRedirect(HTTPRequest $request)
Given request object determine if we should redirect.
throwRedirectIfNeeded(HTTPRequest $request = null)
Handles redirection to canonical urls outside of the main middleware chain using HTTPResponseException.
Will not do anything if a current HTTPRequest isn't available
protected HTTPRequest|null
getOrValidateRequest(HTTPRequest $request = null)
Return a valid request, if one is available, or null if none is available
protected bool
requiresSSL(HTTPRequest $request)
Check if a redirect for SSL is necessary
protected
requiresTrailingSlashRedirect(HTTPRequest $request, string $url)
Check if a redirect for trailing slash is necessary
int
getRedirectType()
No description
$this
setRedirectType(int $redirectType)
No description
array|bool
getEnabledEnvs()
Get enabled flag, or list of environments to enable in.
$this
setEnabledEnvs(array|bool $enabledEnvs)
Set enabled flag, or list of environments to enable in.
Note: CLI is disabled by default, so "cli"(string)
or true(bool)
should be specified if you wish to
enable for testing.
protected
isEnabled()
Ensure this middleware is enabled
protected bool
hasBasicAuthPrompt(HTTPResponse $response = null)
Determine whether the executed middlewares have added a basic authentication prompt
protected HTTPResponse
redirectToScheme(HTTPRequest $request, string $scheme, string $host = null)
Redirect the current URL to the specified HTTP scheme