views:

47

answers:

1

How do I change a period/dot (.) into a plus (+) of a match using mod_rewrite? My match is #2.

This is my rule so far:

RewriteRule ^(.*)/(.*)-[0-9]*\.shtml$ http://kentwired.com/search/most-popular/?searchphrase=exact&searchword=$2 [L,R=301]

With my rules currently,

http://media.www.kentnewsnet.com/media/storage/paper867/news/2009/12/11/News/Tough.Economy.Leaves.Bodies.Unburried-3850836.shtml

becomes

http://kentwired.com/search/most-popular/?searchphrase=exact&searchword=Tough.Economy.Leaves.Bodies.Unburried

but I need

http://kentwired.com/search/most-popular/?searchphrase=exact&searchword=Tough+Economy+Leaves+Bodies+Unburried

+1  A: 

Try this:

RewriteRule ^([^.]*)\.(.*\.shtml)$ /$1+$2 [N]
RewriteRule ^.+/([^/]+)-[0-9]*\.shtml$ http://kentwired.com/search/most-popular/?searchphrase=exact&searchword=$1 [L,R=301]
Gumbo
Please bear with me: I'm a Mod-Rewrite-novice to put it mildly, but doesn't your solution simply replace `Tough.Economy.Leaves.Bodies.Unburried` with `Tough+Economy.Leaves.Bodies.Unburried` instead of the desired `Tough+Economy+Leaves+Bodies+Unburried` (which may be impossible in one search and replace)?
Bart Kiers
@Bart K.: No, the *N* flag will restart the rewriting process immediately until all dots are replaced. But the *N* flag can also be dangerous as it doesn’t increment the internal redirection counter that is to avoid infinite recursions. So you might also try *L* instead.
Gumbo
Thank you for you explanation (even though I am not the one asking the question)!
Bart Kiers
It seems just want to spin....
Daniel A. White
@Daniel A. White: mod_rewrite is not a good tool to do this kind of work. Better use an external script to do the replacement.
Gumbo
@Gumbo - do you have any php that would do the trick?
Daniel A. White
@Daniel A. White: You can almost use the same with PHP: `if (preg_match('~^.*/(.*)-[0-9]*\.shtml(?:\?|$)~', $_SERVER['REQUEST_URI'], $match)) header('Location: http://kentwired.com/search/most-popular/?searchphrase=exact`
Gumbo