Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a URL rewrite RegExp

Status
Not open for further replies.

dCyphr

Programmer
Jan 17, 2008
8
US
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:

=> => => => => => => => => => =>
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?
 
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..
 
Hi,

This is really Apache specific question.

But anyway what i can think of is
Code:
/^(.+)?\/\/(.+)?(\.domain1\.com)\/(.+)$/
to 
$1$2$3/userfiles/site1/$4


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
I would also probably track down a regex solution. However, check out the URI processing module. The below script does the translation that you want, and it was the extreme benefit of being readable. Note that at the bottom of the URI perldoc a regex alternative is suggested.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]URI[/green][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$domain[/blue] = [red]'[/red][purple]domain1.com[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$pathpre[/blue] = [red]'[/red][purple]/userfiles/site1[/purple][red]'[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[black][b]my[/b][/black] [red]([/red][blue]$url[/blue], [blue]$expected[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]/[/red][purple][purple][b]\s[/b][/purple]+=>[purple][b]\s[/b][/purple]+[/purple][red]/[/red][red];[/red]
	
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$url[/blue] ... [/purple][red]"[/red][red];[/red]
	
	[black][b]my[/b][/black] [blue]$uri[/blue] = URI->[maroon]new[/maroon][red]([/red] [blue]$url[/blue] [red])[/red][red];[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$uri[/blue]->[maroon]host[/maroon] !~ [red]/[/red][purple](?:^|[purple][b]\.[/b][/purple])[purple][b]\Q[/b][/purple][blue]$domain[/blue][purple][b]\E[/b][/purple]$[/purple][red]/[/red][red])[/red] [red]{[/red]
		[black][b]print[/b][/black] [red]"[/red][purple]not changed (different domain)[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red][blue]$uri[/blue]->[maroon]path[/maroon] =~ [red]/[/red][purple]^[purple][b]\Q[/b][/purple][blue]$pathpre[/blue][purple][b]\E[/b][/purple](?:[purple][b]\/[/b][/purple]|[blue]$)[/blue][/purple][red]/[/red][red])[/red] [red]{[/red]
		[black][b]print[/b][/black] [red]"[/red][purple]not changed (path ok)[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
		
	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[gray][i]# Set new Path[/i][/gray]
		[black][b]my[/b][/black] [blue]$newpath[/blue] = [red]'[/red][purple]/userfiles/site1[/purple][red]'[/red] . [blue]$uri[/blue]->[maroon]path[/maroon][red];[/red]
		[blue]$uri[/blue]->[maroon]path[/maroon][red]([/red][blue]$newpath[/blue][red])[/red][red];[/red]

		[blue]$url[/blue] = [blue]$uri[/blue]->[maroon]as_string[/maroon][red];[/red]

		[olive][b]if[/b][/olive] [red]([/red][blue]$url[/blue] eq [blue]$expected[/blue][red])[/red] [red]{[/red]
			[black][b]print[/b][/black] [red]"[/red][purple]ok[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
		[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
			[black][b]print[/b][/black] [red]"[/red][purple]bad[purple][b]\n[/b][/purple][purple][b]\t[/b][/purple]expected: [blue]$expected[/blue][purple][b]\n[/b][/purple][purple][b]\t[/b][/purple]found:    [blue]$url[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
			[olive][b]last[/b][/olive][red];[/red]
		[red]}[/red]
	[red]}[/red]
[red]}[/red]


[teal]__DATA__[/teal]
[teal][URL unfurl="true"]http://www.domain1.com[/URL]      =>      [URL unfurl="true"]http://www.domain1.com/userfiles/site1[/URL][/teal]
[teal][URL unfurl="true"]http://domain1.com[/URL]      =>      [URL unfurl="true"]http://domain1.com/userfiles/site1[/URL][/teal]
[teal][URL unfurl="true"]http://ww2.domain1.com[/URL]      =>      [URL unfurl="true"]http://ww2.domain1.com/userfiles/site1[/URL][/teal]
[teal][URL unfurl="true"]http://www.domain1.com/[/URL]      =>      [URL unfurl="true"]http://www.domain1.com/userfiles/site1/[/URL][/teal]
[teal][URL unfurl="true"]http://www.domain1.com/folder/test.htm[/URL]      =>      [URL unfurl="true"]http://www.domain1.com/userfiles/site1/folder/test.htm[/URL][/teal]
[teal][URL unfurl="true"]http://domain1.com/test.htm?param=abc[/URL]      =>      [URL unfurl="true"]http://domain1.com/userfiles/site1/test.htm?param=abc[/URL][/teal]
[teal][URL unfurl="true"]http://ww8.domain1.com/?param=123[/URL]      =>      [URL unfurl="true"]http://ww8.domain1.com/userfiles/site1/?param=123[/URL][/teal]
[teal][URL unfurl="true"]http://www.domain1.com/userfiles/site1/folder/test.htm[/URL]      =>      [URL unfurl="true"]http://www.domain1.com/userfiles/site1/folder/test.htm[/URL][/teal]
[teal][URL unfurl="true"]http://www.domain1.com:8080/[/URL]      =>      [URL unfurl="true"]http://www.domain1.com:8080/userfiles/site1/[/URL][/teal]
[teal][URL unfurl="true"]http://www.domain1.com:8443/folder/test.htm[/URL]      =>      [URL unfurl="true"]http://www.domain1.com:8443/userfiles/site1/folder/test.htm[/URL][/teal]
[teal][URL unfurl="true"]https://domain1.com/test.htm?param=abc[/URL]      =>      [URL unfurl="true"]https://domain1.com/userfiles/site1/test.htm?param=abc[/URL][/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Other Modules used :
[ul]
[li]URI[/li]
[/ul]
[/tt]

- Miller
 
PS.

Line 23 should be the following. If I'm going to declare data variable, I might as well use it:

Code:
		[gray][i]# Set new Path[/i][/gray]
		[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$newpath[/blue] = [blue]$pathpre[/blue] . [blue]$uri[/blue]->[maroon]path[/maroon][red];[/red]

- Miller
 
Excellent help. Thanks it's exactly what I needed!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top