Solved

mod_rewrite Replace Underscores with hyphens in URL

Posted on 2016-09-16
4
44 Views
Last Modified: 2016-11-02
Hello,

I have a RewriteRule that currently modifies a URL to be more SEO friendly

RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}

rule above will take this url and modify it

index.php?cPath=277_280

which would result in something like this:

replacement-parts-c-277_280_295.html

I need it to look like this:

replacement-parts-c-277-280-295.html

what Is for the above Rewrite rule to also replace the underscore with a dash.  I have attempted several changes, but I'm failing at regex syntax.  Is there a way to do this within the one rule above?
0
Comment
Question by:FourMat
4 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41802931
Is the existing rule working (except for the underscores for dashes part)?

I'm guessing not, but I wanted to check.
0
 
LVL 42

Accepted Solution

by:
David S. earned 500 total points
ID: 41803155
You can use the "N" (next) flag to loop and replace characters. (Care must be taken not to create an endless loop!) You can also use the "QSA" (query string append) flag to combine the new query string with any that might already be present. The first rule replaces all underscores except for the last one, which is left for the following non-looping rule to match.
RewriteRule ^(.*-c-[^/_]*?)_([^/_]*?_[^/]*\.html)$ $1-$2 [N]
RewriteRule ^.*-c-([^/_]*?)_([^/_]*)\.html$ index.php?cPath=$1-$2 [QSA]

Open in new window

Mod_Rewrite Documentation
0
 

Author Comment

by:FourMat
ID: 41817830
David, and Terry, thanks for the responses.  I haven't had a chance to test these ideas (got pulled away on another project briefly)  but will definitely be back on it soon and test, thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now