Solved

mod_rewrite Replace Underscores with hyphens in URL

Posted on 2016-09-16
4
51 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

If you don’t want your company's site to fail on the web, you’d do well to observe these best web design practices and make sure you implement them when applicable.
SEO can be a real minefield to navigate, but there are three simple ways to up your SEO game just be re-assessing your content output.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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