Forum Moderators: phranque

Message Too Old, No Replies

redirect subdomain to new URL

         

poleary

4:09 pm on Jan 23, 2012 (gmt 0)

10+ Year Member



OK, so I have an apache server that houses about 15 clients that all have their own URL clientX.example.com.
Each of these clients have their own conf file under /etc/httpd/conf.d/ with a VirtualHost section per below:
<VirtualHost 11.22.33.44:80>
ServerName client1.example.com
ErrorLog logs/error-client1.example.COM.log
TransferLog logs/access-client1.example.com.log
ServerAdmin webmaster@client1.example.com
DocumentRoot "/var/www/html/client1"
</VirtualHost>

So, what I really want to do is take the connection to client1.example.com and send them to the URL login.example.com/client1

I have tried several rewrites, but none seem to give the desired results of having them redirected and change the browser URL.

I have used the following lines in the above stanza:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^login\.asccontracts\.com
Rewriterule ^(client1)\.example\.com/ [login.example.com...] [R=301,L]

I would have thought that would match my client page and sent it over to the new URL, but it never changes the URL...I still get to the docroot fine.

Eventually I would like to use a generic rule for all my virtual hosts similar to:
Rewriterule ^(.*)\.example\.com [login.example.com...]

This seems like something so simple to do and I'm not sure where I am making a mistake.

lucy24

5:48 pm on Jan 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Quick and simple: RewriteRule does not see the host/domain. You have to put that into a Condition.

There may well be other issues, but that's the biggest one.

g1smd

5:55 pm on Jan 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your rule would work only if the requested URL was:
http://<somedomain>.com/client1.example.com/


where
<somedomain>
is NOT
login.asccontracts.com


and the rule would redirect the user to:
https://login.example.com/client1
.

What do you mean by "send them"? That's ambiguous. Do you want an internal rewrite or an external redirect? Be sure you know the difference.

poleary

6:36 pm on Jan 23, 2012 (gmt 0)

10+ Year Member



ah ok, that helps a bunch...I do want an external redirect as I want the URL in the browser to change...an its an external that does this?

So I rewrote my conditions and rule to the following and I seem to get what I want:

RewriteCond %{HTTP_HOST} !^login\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
Rewriterule / [login.example.com...] [R=301,L]

g1smd

7:00 pm on Jan 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Never use
(.*)
at the beginning or in the middle of a RegEx pattern. Change
(.*)
to some other pattern, such as
([^.]+)
or similar.

RewriteRule /
means the requested path part as presented to the RegEx parser must "contain" a slash. This will be true for all requests when this rule is in the httpd.conf file.

When the rule is in .htaccess, the presented pattern will not "begin" with a slash because the leading slash of the requested path is stripped before being presented to the RegEx parser. Mod_Rewrite pattern matching works in "per-directory" context in .htaccess. To be clear, in this case the pattern will match only when the requested URL contains TWO slashes.

poleary

7:32 pm on Jan 23, 2012 (gmt 0)

10+ Year Member



OK, I understand this is the case in .htaccess, but my rules are inside a virtualhost inside an included .conf file. What would the behavious be in this case?