Marty
Marty

Reputation: 2666

RegEx match replace help

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.

Upvotes: 3

Views: 1466

Answers (6)

localshred
localshred

Reputation: 2233

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.

Upvotes: 1

Tim
Tim

Reputation: 1874

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?

Upvotes: 0

Ben
Ben

Reputation: 6777

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.

Upvotes: 1

Gumbo
Gumbo

Reputation: 655815

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).

Upvotes: 2

Ben
Ben

Reputation: 6777

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.

Upvotes: 4

GateKiller
GateKiller

Reputation: 75967

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?

Upvotes: 1

Related Questions