Solved

mod_rewrite Replace Underscores with hyphens in URL

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Wondering how you can get your new website ranking on Google Search so that people find you online?  The answer is Search Engine Optimization (SEO). So how do you Start Ranking a New site with SEO?  Below is a starter guide to get your new website S…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to dynamically set the form action using jQuery.

863 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

21 Experts available now in Live!

Get 1:1 Help Now