Solved

mod_rewrite Replace Underscores with hyphens in URL

Posted on 2016-09-16
4
27 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now