Forum Moderators: phranque
[edited by: not2easy at 7:14 pm (utc) on Jul 9, 2019]
[edit reason] (hopefully) readability [/edit]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
I like to put in an exception for robots.txt, but that's optional. Redirect permanent / https://www.example-site.co.uk/
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.examplesite.co.uk [OR]
RewriteCond %{SERVER_NAME} =examplesite.co.uk
RewriteRule ^ https://www.example-site.co.uk%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^(www\.example-site\.co\.uk)?$ [NC]
RewriteRule (.*) https://www.example-site.co.uk/$1 [R=301,L]
not working
http://example-site.co.uk (non-www http main site)
<VirtualHost nnn.nn.nn.nnn:80>
ServerName www.example-site.co.uk
DocumentRoot /srv/site
ServerAlias example-site.co.uk
Redirect permanent / https://www.example-views.co.uk/
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example-site.co.uk [OR]
RewriteCond %{SERVER_NAME} =example-site.co.uk
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Can anyone tell me where I've gone wrong, please?
... when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.
What do you mean by "fragment"?An in-page element expressed by # in the URL, referring to an <id> or <a name> in the html. If a redirect target includes a fragment (as when you combine multiple pages into one) you need the [NE] flag to keep the # character from getting percent-encoded. This is by far the most common reason for the flag; in fact I think it's the only example given in the docs.
HTTP_HOST can include port identifiers if not the defaultYes, that's exactly why a canonicalization redirect includes both opening and closing anchors. Requests can of course be handled by whatever port suits your fancy--but it shouldn't be part of the request, like
It's difficult to know what is being received because I cannot discover how to log the rejects; they do not appear in the site log nor the error log.Yes, this one's tricky. The only way I have ever figured out to log all details of redirected requests is to replace the R=301 with a rewrite to a quickie php script that first logs headers (including host and https) and then issues the redirect ... but this is obviously taking you into shooting-flies-with-an-elephant-rifle territory. But when a request receives a 301 response, it should definitely be showing up in the ordinary access logs, even if the said logs don't contain the information that would tell you why it was redirected.
<VirtualHost nnn.nn.nn.nnn:80>
ServerName www.example-site.co.uk
DocumentRoot /srv/site
ServerAlias example-site.co.uk
RewriteEngine on
RewriteCond %(HTTPS) !on [OR]
RewriteCond %{HOST_HTTP} !^example-site.co.uk$
RewriteRule (.*) https://www.example-site.co.uk/$1 [R=301,L]
</VirtualHost> [edited by: phranque at 10:51 pm (utc) on Jul 12, 2019]
[edit reason] readability [/edit]
<VirtualHost nnn.nn.nn.nnn:443>
ServerName example-site.co.uk
DocumentRoot /srv/site
Redirect permanent / https://www.example-site.co.uk/
</VirtualHost> [edited by: phranque at 10:48 pm (utc) on Jul 12, 2019]
[edit reason] unlinked urls [/edit]
By the way, current virtual_host config for port 80 is...
<VirtualHost nnn.nn.nn.nnn:80>
Listen 80
ServerName www.example-site.co.uk
DocumentRoot /srv/site
ServerAlias example-site.co.uk
RewriteEngine on
RewriteRule (.*) https://www.example-site.co.uk/$1 [R=301,L]
</VirtualHost>
# hyphenless http
<VirtualHost nnn.nn.nn.nnn:80>
ServerName www[.]examplesite[.]co[.]uk
DocumentRoot /srv/site
ServerAlias examplesite[.]co[.]uk
Redirect permanent / https[://]www[.]example-site[.]co[.]uk/
</VirtualHost>
# hyphenless https
<VirtualHost nnn.nn.nn.nnn:443>
ServerName www[.]examplesite[.]co[.]uk
DocumentRoot /srv/site
ServerAlias examplesite[.]co[.]uk
Redirect permanent / https[://]www[.]example-site[.]co[.]uk/
</VirtualHost>
# hypen (real domain) http
<VirtualHost nnn.nn.nn.nnn:80>
ServerName www[.]example-site[.]co[.]uk
DocumentRoot /srv/site
ServerAlias example-site[.]co[.]uk
Redirect permanent / https[://]www[.]example-site[.]co[.]uk/
</VirtualHost>
<VirtualHost nnn.nn.nn.nnn:443>
ServerName example-site[.]co[.]uk
DocumentRoot /srv/site
Redirect permanent / https[://]www[.]example-site[.]co[.]uk/
</VirtualHost>
# https - the web site
<VirtualHost 185.35.151.121:443>
(etc)