Forum Moderators: phranque

Message Too Old, No Replies

Wildcard subdomain redirect

         

pjdelsh007

2:20 pm on Mar 10, 2008 (gmt 0)

10+ Year Member



I've got wildcard DNS working. How do I setup mod_redirect to

[username.mydomain.com...] --> [mydomain.com...]

jdMorgan

5:03 pm on Mar 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this search [google.com] to get started.

Jim

pjdelsh007

3:35 pm on Mar 23, 2008 (gmt 0)

10+ Year Member



I can't get this to work with HTTP.CONF. What am I doing wrong?

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

jdMorgan

4:29 pm on Mar 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What, exactly, seems to be the problem?
How did you test?
What were the results?
How did those results differ from your expectations?
Is there anything relevant in your server error log?

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

pjdelsh007

4:56 pm on Mar 23, 2008 (gmt 0)

10+ Year Member



1. redirect is not working
[username.mydomain.com...] is not being directed to [mydomain.com...]

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.

jdMorgan

5:29 pm on Mar 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can only assume you've got all the other configuration directives needed to define this vHost and allow mod_rewrite to execute. If not, look into the DocumentRoot and Options directives -- You must set "Options FollowSymLinks" or "Options SymLinksIfOwnerMatch" in order to enable mod_rewrite.

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]

but again, there were no fatal problems with what you had. This just covers all of the cases of example.com, www.example.com, username.example.com, www.username.example.com, username.www.example.com -- and for users who really love to type "w", www.username.www.example.com...

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]

Request foo.html from your server, and you should land at google.

Jim

pjdelsh007

3:03 pm on Mar 24, 2008 (gmt 0)

10+ Year Member



I got the rewrite working! But it appears the SSL redirect is mutualy exclusive with subdomain redirect

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>

jdMorgan

4:48 pm on Mar 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add a test to set "http" or "https" as the protocol, depending on the requested port:

# 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]

I notice that you keep dropping the escape characters that I've added preceding literal periods, and ignoring other subtle changes I've made in the code. It's not going to work properly as a system unless it all goes together.

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

pjdelsh007

6:09 pm on Mar 24, 2008 (gmt 0)

10+ Year Member



Thanks for all the feedback Jim. Pardon the ignorance, I'm a IIS guy with no PHP experience trying to transistion to Apache on W2K3, and then going to RHEL or SUSE in the future.

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.

jdMorgan

4:34 pm on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, here's the usual approach:

# 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]

The special conditions in the second rule are needed to prevent "mixed secure/non-secure" warnings on your SSL pages, which can kill your site's "trust factor" with users. If your SSL/non-SSL-shared included objects are well-organized into directories, then this simple approach may be all you need. Otherwise, you may need to exclude them by filetype or by a mixture of filetype and directory location. If this gets unwieldy, then you may need to re-organize these shared files to make exclusion easier.

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

pjdelsh007

5:35 am on Mar 30, 2008 (gmt 0)

10+ Year Member



Ok Jim, almost there!

Recap - I'm trying to:
1- Force all users to SSL for the entire site
2- Redirect wildcard subdomains

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!

pjdelsh007

11:33 am on Apr 1, 2008 (gmt 0)

10+ Year Member



:bump:

Should I subscribe?

jdMorgan

1:24 pm on Apr 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, but I don't see anything wrong, so unless someone else comes along and spots something, it'll be up to you to understand the code and the problem, and modify the code to suit your needs -- We can really go only so far trying to debug subtle problems in a discussion forum environment. :(

Jim

pjdelsh007

4:22 pm on Apr 3, 2008 (gmt 0)

10+ Year Member



It's working! A developer in India came up with

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!