views:

34

answers:

3

I need to have a RegEx that will match a URI like this based on the subdomain "blog"--

http://blog.foo.com/2010/06/25/city-tax-sale/

and redirect like this (getting rid of the subdomain and numbers/date)--

http://foo.com/city-tax-sale/

where the last bit "city-tax-sale" would be a wildcard. So basically any incoming URI that starts with 'blog.foo.com' would be redirected to 'foo.com' + 'whatever is at the end of the above URI after the three sub paths with numbers.

I hope that makes sense. Just trying to create one redirect instead of writing every single one.

A: 

You can try this:

/http:\/\/blog\..*\.[a-zA-Z]{2,5}\/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/(.*)\//
xil3
this will rewrite not redirect.
galambalazs
+2  A: 
RewriteCond %{HTTP_HOST} ^blog.foo.com [NC] 
RewriteRule ^(\d+/)+(.*)/?$ http://foo.com/$2 [L,R=301]
galambalazs
+3  A: 

This will explicitly match your date format, rather than any series of digits and slashes:

RewriteCond %{HTTP_HOST} ^blog\.foo\.com$ [NC]
RewriteRule ^/\d{4}/\d{2}/\d{2}/(.*)$ http://foo.com/$1 [L,R=301]

The regex part can be broken does to:

^      # start of non-domain url
/\d{4} # slash followed by 4 digits
/\d{2} # slash followed by 2 digits
/\d{2} # slash followed by 2 digits
/      # closing slash
(.*)   # rest of the url, captured to group 1
$      # end of url

With the $1 in the replacement being group 1.

In the options part:
L is for "Last" - tells it to not bother looking at other rules.
R=301 is for Redirect with 301 header, which means permanent redirect (just R would send a temporary 302 header)

The RewriteCond bit performs a case-insensitive (NC option) check on the HTTP_HOST header (supplied by user/client) and if it starts blog.foo.com it performs the rewrite, otherwise it doesn't.

Peter Boughton
*"So basically **any incoming URI that starts with 'blog.foo.com'** would be redirected to 'foo.com"*
galambalazs
Ah, missed the //blog.foo.com -> //foo.com part.
Peter Boughton
Ok, fixed that now. Also since RewriteCond is a regex, I escaped the `.`s and added an ending marker - still works without, but meh.
Peter Boughton
it's still more specific than what the OP asked for. :)
galambalazs
Thanks, this is perfect and probably eliminated any follow up questions! Cheers!
Marty