Forum Moderators: phranque
[edited by: not2easy at 2:32 am (utc) on Mar 10, 2018]
[edit reason] unlinked/readability [/edit]
The ServerName directive specified within the VirtualHost gives the primary host name by which the site is identified. If the same site can be accessed by other names, the ServerAlias directive can be used to list them explicitly, or by using a wildcard pattern.
What is not obvious however is if there are any host names by which the server IP is addressable and they are not covered by a ServerName or ServerAlias directive, rather than Apache giving an error, it will route the request to the first VirtualHost definition it found when it read its configuration.
Redirect Permanent / https://www.original-domain.com
Redirect Permanent http://www.original-domain.com https://www.original-domain.com
Redirect Permanent http://original-domain.com https://www.original-domain.com
Redirect directives take precedence over Alias and ScriptAlias directives, irrespective of their ordering in the configuration file. Redirect directives inside a Location take precedence over Redirect and Alias directives with an URL-path.which to me explained why the server's alias setting might be over-riding the Redirect Permanent settings.
<If "%{HTTP_HOST} == 'www.example.com'">
Redirect permanent "/" "https://www.example.com/"
</If>
<If "%{HTTP_HOST} == "example.com'">
Redirect permanent "/" "https://www.example.com/"
</If>
There is a redirect in my original sites Virtualhost that redirects to https. If I comment it out it solves the problem. But now if you go to httq://original-domain it exposes the folders to the public (not good!).
There is a known problem of using both mod_rewrite with mod_alias in settings.
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.
RewriteCond {HTTP_HOST} !^(www\.example\.com)?$ [NC,OR]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
i would suggest using something like this:And if you're feeling really brave, leave off the anchors in the pattern. By default, regular expressions start as soon as they can and continue as long as they can. But do watch out for leading slashes in the capture if the RewriteRules are lying loose in your <VirtualHost> envelope, as opposed to in a <Directory> section. You don't want to end up with two slashes.
where is the <VirtualHost> container for hostname original-domain listening to Port 80?
i would suggest using something like this:
!^(www\.example\.com)?$ is general. That is anything that isn't "www.example.com" will be redirected. In most situation this works but in my specific case, because of mod_wsgi, requesting www.example2.com will be redirected to httqs//:www.example.com. Why are you doing any of this with mod_alias?
For sites running on a port other than 80:
RewriteCond "%{HTTP_HOST}" "!^www\.example\.com" [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteCond "%{SERVER_PORT}" "!^80$"
RewriteRule "^/?(.*)" "http://www.example.com:%{SERVER_PORT}/$1" [L,R,NE]And for a site running on port 80
RewriteCond "%{HTTP_HOST}" "!^www\.example\.com" [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)" "http://www.example.com/$1" [L,R,NE]If you wanted to do this generically for all domain names - that is, if you want to redirect example.com to www.example.com for all possible values of example.com, you could use the following recipe:
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)" "http://www.%{HTTP_HOST}/$1" [L,R,NE]
These rulesets will work either in your main server configuration file, or in a .htaccess file placed in the DocumentRoot of the server.
Solution:
The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).
can be unwieldy as a site grows and develops.