views:

541

answers:

11

I am trying to do a regex match and replace for an .htaccess file but I can't quite figure out the replace bit. I think I have the match part right, but maybe someone can help.

I have this url- http://www.foo.com/MountainCommunities/Lifestyles/5/VacationHomeRentals.aspx

And I'm trying to turn it into this- http://www.foo.com/mountain-lifestyle/Vacation-Home-Rentals.aspx

(MountainCommunities/Lifestyles)/\d\/(.*)(.aspx)

and then I figured I would have a rewrite rule starting like this-

mountain-lifestyle/$2$3

but I need to take what is in $2 in this instance and rewrite it to place dashes between the words with capital letters. Now I'm stumped.

+1  A: 

I don't think what your doing with the capital letters is possible with regex...

You would be better keeping the dashes in the URL and removing the .aspx

eg: http://www.foo.com/MountainCommunities/Lifestyles/5/Vacation-Home-Rentals

This would require the following rule:

^/MountainCommunities/Lifestyles/5/([^/]+)/\?([^/]+)   /mountain-lifestyle/$1.aspx?$2 [I]

This also takes into account any querystrings that are sent to the page.

BTW: How are you using .htaccess with IIS?

GateKiller
The Query String is not part of the URL path and thus cannot be checked within the RewriteRule directive.
Gumbo
@Gumbo: Of course it is. I'm current use ISAPI_rewrite to do such things.
GateKiller
+1  A: 

Actually I'm trying to get the dashes INTO the URL and was going to use the capital letters as the way to tell regex where to put them.

I'm using ISAPI_rewrite on IIS. Handy little tool.

Marty
I'm still not totally sure this is possible. I think it would require an overly complex regex to accomplish. Is there any particular reason why you need to put the dashes into the URL?
GateKiller
I'm not sure he can see comments until he gets a certain amount of rep.
Teifion
@Teifion: You can see them at any rep. But you can't add your own 'til 50.
Gordon Wilson
+3  A: 

I think you'll have to do it in two bits... Take out $2, precede every capital (apart from the first) with a -, then use just append the result to http://www.foo.com/mountain-lifestyle/ with a .aspx on the end.

Ben
A: 

yes, but that's the part i'm stumped on, i can't figure out how to do that? :)

Marty
+2  A: 

Try this:

RewriteRule ^(([A-Z][a-z]+-)*)([A-Z][a-z]+)(([A-Z][a-z]+)+)(\.aspx)?$ /$1$3-$4 [N]
RewriteRule ^([A-Z][a-z]+-)+[A-Z][a-z]+$ /$0.aspx [R=301]

Note that mod_rewrite uses an internal counter to detect and avoid infinit loops. So your URL may not contain too much words having to be converted (see MaxRedirects option for Apache < 2.1 and LimitInternalRecursion directive for Apache ≥ 2.1).

Gumbo
+1  A: 

You can use the regular expression "([A-Z])" on the middle bit "VacationHome", replacing with the regex "-$1" - This will give you "-Vacation-Home-Rentals" - Then you can just chop off the first character, and stick the first part of the URL on the front, and .aspx on the end.

Ben
A: 

so something like this?

MountainCommunities/Lifestyles/\d\/([A-Z]).aspx mountain-lifestyle/-$1$2

When I run that through my regex checker though it doesn't match on the url string and I'm still not sure how to chop off the preceding dash. With [\b] or ?

Marty
A: 

I don't think I quite understand what you are trying to do. Why can't you simply search for:

http://www.foo.com/MountainCommunities/Lifestyles/5/VacationHomeRentals.aspx

and replace it with:

http://www.foo.com/mountain-lifestyle/Vacation-Home-Rentals.aspx ?

Or is this a specific example of a patten you are trying to transform?

Tim
A: 

Yes, because I have over 100 links with similar patterns..

Marty
+1  A: 

I think the main regex has been written by others, but to match the request name to place dashes (assuming all the file names have a three-name camel cased representation ala 'VacationHomeRentals.aspx':

RewriteRule: ^/MountainCommunities/Lifestyles/\d+/([A-Z][a-z]+)([A-Z][a-z]+)([A-Z][a-z]+)\.aspx$ /mountain-lifestyle/$1-$2-$3.aspx

This is a restricted version of @Gumbo's response, as I have not had a chance to test his recursion. The recursion technique is definitely the best and most usable for any scenario.

localshred
A: 

okay @localshred

That makes sense, but what if not all have 3? Some have 2 and some 4? Appreciate everyone's continued help!

Marty