Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite reg. expression replacing

         

madmac

8:58 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



I have not for the life of me been able to find a definitive answer on this question:

Does mod_rewrite support reg. expression replacing?

For example, in a given rule, replace all occurrences of X in matched pattern 1 with Y.

jd01

9:03 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The short answer is NO.

The longer answer is depending on the application, sometimes there is a work-around, but generally, it is faster/earier to use a script before the URL is requested to do the replacing.

If you would post an example, we might be able to give you some advice.

Hope this helps.

Justin

madmac

9:13 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



right, that is what I am doing right now is using a script to do the replacement if needed...

However, I was curious, as if possible with just mod_rewrite, it would be a more simple and elegant solution than mod_rewrite + a script where needed.

A short example of where I use it is the following:

urls like mysite/some_item_name should be changed to mysite/some-item-name

jd01

9:17 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is possible, and there are some recent threads that cover the implementation of it in the .htaccess file.

You might try a search on your favorite engine for:
replacing underscores in URL site:webmasterworld.com

I would post them for you, but they are probably 5 to 10 pages back and I don't remember exactly where they are.

Justin

madmac

9:39 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



without looking, I'm guessing that you mean I could match items split by "_", and have rules for up to the number of items I may have separated by "_" and then rejoin them on "-" with the rule...

That's not really what I'm after, because it would take a large number of rules just to perform that, and also the number of rules I would need is unknown.

If that's the only known solution in mod_rewrite, I think I'll just stick to the script for that part of the url mod ;)

jd01

10:38 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sticking with what is working is probably the best idea, because you are right about the number of rules, etc.

Justin