[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

mod_rewrite Replace Underscores with hyphens in URL

Posted on 2016-09-16
4
Medium Priority
?
94 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 43

Accepted Solution

by:
David S. earned 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Online promotion is consistently becoming more important for all types of businesses. From Facebook ads to search engines to YouTube videos, there are all sorts of channels that can effectively be used to promote a business or product. But how shoul…
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

650 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