Forum Moderators: phranque
[username.mydomain.com...] --> [mydomain.com...]
<VirtualHost *>
ServerAlias www.mydomain.com
ServerName www.mydomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule ^(.*)$ /www/$1 [L]
RewriteCond %{HTTP_HOST} ^www.*
RewriteRule ^(.*)$ /www/$1 [L]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com
RewriteRule ^(.*)$ /%1/$1 [L]
</VirtualHost>
The mod_rewrite code is not optimal, but the only really 'fatal' problem I see is that you have not made any provisions for the wildcard subdomains in your ServerAlias directive.
Jim
2. I made the changes to HTTPD.conf and restarted Apache
3. no result, all subdomains continue pointing to the root
4. nothing in the error logs
I've changed the alias to *.mydomain.com but no luck
I appreciate the help Jim
-P.J.
Other than that, I'd clean up the code a bit, like this:
# Canonicalize domain
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
#
# Canonicalize subdomains
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)(\.www)?\.example\.com
RewriteCond %2 !^www
RewriteRule ^/(.*)$ http://%2.example.com/$1 [R=301,L]
#
# Internally rewrite subdomains to subdirectories
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^/(.*)$ /%1/$1 [L]
I assume that your preferred canonical domain name is www.example.com. If not, you will need to adjust the first redirect, and add a second internal rewrite rule.
If you have no other working rewriterules in your httpd.conf vHost container, I'd suggest you start with a single dirt-simple rule, test it, and get it working first:
RewriteRule ^/foo.html$ http://www.google.com/ [R=302,L]
Jim
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) [%{SERVER_NAME}...] [L,R]
How can I redirect all users to SSL, and also redirect wildcard subdomains?
_______
<VirtualHost 10.10.10.10>
DocumentRoot C:/Apache2/htdocs
ServerName mydomain.org
ServerAlias *.mydomain.org
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) [%{SERVER_NAME}...] [L,R]
RewriteCond %{HTTP_HOST} ^mydomain.org
RewriteRule ^(.*)$ /website/$1 [L]
RewriteCond %{HTTP_HOST} ^website.*
RewriteRule ^(.*)$ /website/$1 [L]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.org
RewriteRule ^(.*)$ /%1/$1 [L]
</VirtualHost>
# Force all traffic to https (This will make your site slow!)
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
#
# Canonicalize domain
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦80>s)$
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^/(.*)$ http%2://www.example.com/$1 [R=301,L]
#
# Canonicalize subdomains
RewriteCond %{SERVER_PORT}>s>%{HTTP_HOST} ^(443>(s)¦80>s)>(www\.)?([^.]+)(\.www)?\.example\.com
RewriteCond %4 !^www
RewriteRule ^/(.*)$ http%2://%4.example.com/$1 [R=301,L]
#
# Internally rewrite two main domains to /website/
RewriteCond %{HTTP_HOST} ^mydomain\.org [OR]
RewriteCond %{HTTP_HOST} ^website\.
RewriteRule ^/(.*)$ /website/$1 [L]
#
# Internally rewrite subdomains to subdirectories
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^/(.*)$ /%1/$1 [L]
Also, I'm not sure about what you're wanting to do with that first rule. If you force all traffic to HTTPS, your site will be unnecessarily slow, because the server and client will both be encrypting and then decrypting everything -- even images. In most cases, it's best to create a 'secure' area of the site based on directory paths or page names and/or filetypes, and leave all the other stuff in HTTP.
If you really do want everything as HTTPS, then the complicated parts of the code above can be largely done away with; All you'll need to do is to use "https:" in every rule that does an external redirect and in every canonical (absolute) on-page link.
The ">" character in some of the rules above is totally arbitrary, and means nothing to mod_rewrite or to the regular-expressions parser. It is used only as a 'visual token' to demarcate multiple variable names and values to make the pattern unambiguous when matching, and to visually-imply concatenation.
Change all broken pipe "¦" characters above to solid pipes before use; Posting on this forum modifies the pipe characters.
Jim
These are my objectives
1- Require SSL for the /SSL directory (not the entire site)
2- Redirect wildcard subdomain:
[subdomain.mydomain.org...] --> [mydomain.org...]
I copy/pasted your code, fixing the ¦ pipes but the redirect doesn't work. I can get both working, but not at the same time.
# Force all /SSL traffic to https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(SSL/.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
#
# Force all non-/SSL traffic back to http (except for images and other included objects)
RewriteCond $1 !^(SSL¦images¦Jscripts)/
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^/(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
I should probably point out that it'll be up to you to study the examples I've posted, understand them, and modify them to suit your needs. All sites are different, and it's almost impossible to communicate the "big picture" of a site as well as all of its details in a forum context. So, while we can discuss "ideas" here, it's almost impossible for me to post code that will solve all of your problems and work right out of the box, except in the very simplest of cases. And that is not the focus of this forum anyway: We're a discussion forum, and not a "help desk."
It's often the case that SSL and non-SSL are put into two separate virtual host configuration containers; Be sure that that is not the case here, since that would require you to 'split' the code above into the two different containers, as appropriate.
Change all broken pipe "¦" characters above to solid pipes before use; Posting on this forum modifies the pipe characters.
Jim
The subdomain redirect should do the following, where xyz is the username
[xyz.mydomain.org...] --> [mydomain.org...]
Currently SSL rewrite is working. But I'm having 2 issues with the subdomain redirect:
1- The redirect requires www.xyz.mydomain.org to work. I don't want the www
2- Browsing to xyz.mydomain.org (http) does not redirect properly. However browsing to [xyz.mydomain.org...] (https) does work. We don't want to require users type in https://
__________________________________________
<VirtualHost 10.10.10.10>
DocumentRoot C:/Apache2/htdocs
ServerName mydomain.org
ServerAlias *.mydomain.org
RewriteEngine on
# Redirect all http requests to https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) [%{SERVER_NAME}...] [L,R]
# Internally rewrite two main domains to /website/
RewriteCond %{HTTP_HOST} ^mydomain\.org [OR]
RewriteCond %{HTTP_HOST} ^website\.
RewriteRule ^/(.*)$ /website/$1 [L]
# Internally rewrite subdomains to subdirectories
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.org
RewriteRule ^/(.*)$ /%1/$1 [L]
</VirtualHost>
_________________________________
So close I can taste it!
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) [%{SERVER_NAME}$1...] [R,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com[/]?$ [NC]
RewriteCond %{HTTP_HOST} ^(.*?)\.domain\.com[/]?$ [NC]
RewriteRule (.*) [domain.com...]
Jim thanks for all your help!