Regex to Replace Spaces in Two Conditions with Isapi ReWrite
-
22-06-2021 - |
Question
I am using Isapi Rewrite for IIS, and I want to make SEO friendly URLs for a dynamic product page.
I need to replace spaces differently in two query string parameters.
In the first param, \s should be replaced with + In the second, all \s should be replaced with -
#seo. 2 conditions. split on _ delimiter.
RewriteRule ^Store/([^_]+)_([^/]+) Store/Details.aspx?id=$1&name=$2 [QSA,NC]
#replace spaces with + in first condition (doesn't work)
#RewriteRule ^Store/([^\w\s]+)\s(.+)$ Store/Details.aspx?id=$1+$2 [QSA, NC]
#replace spaces with dash in second condition ???
Examples
Store/NP SP_name name
//$1: NP+SP
//$2: name-name
// output: Store/NP+SP_name-name
Store/mn%2098%20765_name%20name%20name
//$1: mn+98+765
//$2: name-name-name
//output: Store/mn+98+765_name-name-name
La solution
I've done smth like that the other day, but there was a simplier task with only one type of replacement. Try using the following for basic redirect(if it works, we'll think of a more complex, multiple-parameters scenario):
RewriteRule ^Store/(.+)\s([^_]+)_(.+)\s(.+) /Store/$1+$2_$3-$4 [NC,R=301,L]
Make sure you put in on top of the existing rewrite.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow