Page 1 of 1

Need help with a URL rewrite RegExp

Posted: Fri Jan 18, 2008 12:03 am
by dCyphr
I have a url rewriter on my website and need help making a tight RegExp so I can reroute requests based on the domain name. My website has a root folder called "/userfiles" which is where all the sub websites go under. So the structure is like this:

/userfiles/site1
/userfiles/site2
/userfiles/site3

Say I have a domain called domain1.com that is supposed to go to /userfiles/site1/*.* and no where else. So what I want the result to be is like this:

http://www.domain1.com => http://www.domain1.com/userfiles/site1
http://domain1.com => http://domain1.com/userfiles/site1
http://ww2.domain1.com => http://ww2.domain1.com/userfiles/site1
http://www.domain1.com/ => http://www.domain1.com/userfiles/site1/
http://www.domain1.com/folder/test.htm => http://www.domain1.com/userfiles/site1/folder/test.htm
http://domain1.com/test.htm?param=abc => http://domain1.com/userfiles/site1/test.htm?param=abc
http://ww8.domain1.com/?param=123 => http://ww8.domain1.com/userfiles/site1/?param=123
http://www.domain1.com/userfiles/site1/folder/test.htm => http://www.domain1.com/userfiles/site1/folder/test.htm
http://www.domain1.com:8080/ => http://www.domain1.com:8080/userfiles/site1/
http://www.domain1.com:8443/folder/test.htm => http://www.domain1.com:8443/userfiles/s ... r/test.htm
https://domain1.com/test.htm?param=abc => https://domain1.com/userfiles/site1/test.htm?param=abc

Can anyone see the pattern in here to make a solid RegExp? Below is an entry I put in my rewriter app, but that just sends anything that hits the root will rewrite it the new format carrying all its parameters:

<rewrite url="^(.*)/(\?.+)?$" to="$1/userfiles/site1$2" />

Can anybody help construct or start a single rule that can handle all my examples above specifically for domain1.com?

Re: Need help with a URL rewrite RegExp

Posted: Fri Jan 18, 2008 12:37 am
by dCyphr
Just to give an idea, below is a first draft. As you can tell, I'm not really good at RegEx but I hope someone can see what I'm trying to do. I need to hardcode the domain because there will be one entry per domain like below. Notice each domain is assigned a destination site:

<rewrite url="^(http|https)(.*)(domain1.com:\/\/[^\/]+)/(\?.+)?$" to="$1$2$3/userfiles/site1$4" />
<rewrite url="^(http|https)(.*)(anotherdomain.com:\/\/[^\/]+)/(\?.+)?$" to="$1$2$3/userfiles/site2$4" />
<rewrite url="^(http|https)(.*)(domain5.com:\/\/[^\/]+)/(\?.+)?$" to="$1$2$3/userfiles/site5$4" />

Can someone help with the adjustment? I think I still have a long way to go..

Re: Need help with a URL rewrite RegExp

Posted: Sun Feb 10, 2008 1:48 am
by GeertDD
Here's a PHP test script. I added some comments inside the regex. Hopefully you can port it to your rewriter.

Code: Select all

$str = 'http://www.domain1.com => http://www.domain1.com/userfiles/site1
http://domain1.com => http://domain1.com/userfiles/site1
http://ww2.domain1.com => http://ww2.domain1.com/userfiles/site1
http://www.domain1.com/ => http://www.domain1.com/userfiles/site1/
http://www.domain1.com/folder/test.htm => http://www.domain1.com/userfiles/site1/folder/test.htm
http://domain1.com/test.htm?param=abc => http://domain1.com/userfiles/site1/test.htm?param=abc
http://ww8.domain1.com/?param=123 => http://ww8.domain1.com/userfiles/site1/?param=123
http://www.domain1.com/userfiles/site1/folder/test.htm => http://www.domain1.com/userfiles/site1/folder/test.htm
http://www.domain1.com:8080/ => http://www.domain1.com:8080/userfiles/site1/
http://www.domain1.com:8443/folder/test.htm => http://www.domain1.com:8443/userfiles/s ... r/test.htm
https://domain1.com/test.htm?param=abc => https://domain1.com/userfiles/site1/test.htm?param=abc';
 
$str = preg_replace(
'
    ~^                      # beginning of each line (because of m modifier)
    (                       # start match $1
        https?://           # hard coded "http://" or "https://"
        (?:[a-z0-9]++\.)?   # possible subdomain
        domain1\.com        # hard coded domain1.com
    )                       # end match $1
    (/\S*\b)?               # the rest of the URL (match $2)
    ~imx
', '$1/userfiles/site1$2', $str);
 
echo $str;