Regex to Replace Spaces in Two Conditions with Isapi ReWrite - regex

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

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.

Related

Htaccess redirect matching from a list?

I believe there should be a simpler method to rewriting URLs than I currently have and wonder if anyone can help.
The site I am working on has multiple brands for example:
https://example.com/anything/brand/nike/something
https://example.com/anything/brand/puma/something
My current redirect would be
RedirectMatch 301 "(.*)/brand/nike(.*)$" "$1/manufacturer--nike"
To get me the following output, removing /brand/ and replacing nike with manufacturer--nike and finally removing anything that follows i.e. /something.
https://example.com/anything/manufacturer--nike
Now I could add a second rule for Puma and each of the other brands, but I imagine there is a way to match against a list of brands and use one rule but my Google skills have failed me in finding a solution.
Is there a way?
If the number of brands are limited then you could use alternation (eg. nike|puma|brand etc.) in the regex, to match nike or puma or brand.
Aside: To match nike/<something>, you should at least check for that slash, otherwise it's going to match nikeeeee and nikey etc. (Although not necessarily an issue.)
For example:
RedirectMatch 301 "(.*)/brand/(nike|puma|brand)/" "$1/manufacturer--$2"
The (.*)$ at the end of the regex - to simply remove the trailing part of the URL-path - is not required.
The $2 backreference contains the "brand-name" matched by the second capturing (alternation) subpattern.
method to rewriting URLs
Just to clarify, this is an external redirect, it's not "rewriting URLs".

Matching just the first and second block of an URL

I'm trying to do a regex to match just the second part of a URL and leave the rest behind
For example
https://example.com/first-part/second-part/third-part/?prop=2
result = https://example.com/alt/second-part/
How can I do this?
I'm able to match the first two parts but for when I use the "/" for match it picks the last / one, instead the one before.
I can go the simple way like this:
RewriteRule ^(.*)first-part\/(.+)\/(.*)\/(.*)$ https://example.com/alt/$2 [R=301,L]
The problem is that if the URL is like this:
https://example.com/first-part/second-part/
Result expected. https://example.com/alt/second-part/
It won't even match it
So I'm looking for a more generic alternative, that may match multiple scenarios giving the same result ultimately in the same format:
https://example.com/alt/second-part/
Just knowing how the first-part exactly is and not knowing how anything beyond the second-part will be formated.
Taking into account the recommendations of #Eraklon to avoid the greedy checks I've found out a solution:
RewriteRule ^first-part\/([^\/]+(\/)?)(.*) https://example.com/alt/$1 [R=301,L]
Can be checked here:
https://htaccess.madewithlove.be?share=8973fe68-f137-59a5-b27b-0cbbe3d842bc
It exactly matches the first-part with ^first-part/ and then in enters the group:
([^\/]+(\/)?)
That checks for 1 or more chars that are not a slash /. When it finds the first slash it can be the next section of the URL or the end of the URL.
Not sure if this is the best but the idea is that it matches just 1 pattern for $1 that includes both the end slash and not-slash for the second-part block of the URL.
I've not been able to remove the last bit from the url (the parameters ?parameter=a)
So the result with this form a URL like:
https://example.com/first-part/second-part/third-part/?parameter=a
Will be
https://example.com/alt/second-part/?parameter=a
Fortunately, the parameters are not too bad, but I would have preferred the full solution.

Rewriting a URL to remove certain characters

I need to rewrite some blog URLs to remove certain characters. These are the along the lines of "a556" (a is always present, the numbers are always 3 digits and are random). This is proceeded by either a single or double hyphen, which I also need to remove.
These need to redirect from:
[domain]/blog/[article_name]-a556
or
[domain]/blog/[article_name]--a556
To
[domain]/blog/[article_name_with_characters_removed]
I think the regex to detect the text to be removed is:
([-]{1,2}a[0-9])\w+
But I don't know how to put this into a Rewrite rule.
Can anyone help?
Please try this:
RewriteEngine On
RewriteRule (.*)-{1,2}a\d{3}(.*) $1$2 [R]
Are you looking for a function to process your old URLs into new ones? Something like this should do the trick, if you have an array of URLs:
var processedURLs = oldURLs.map(function(url) {
return url.replace(/[-]{1,2}a[0-9]+/, '');
})
This rewrite rule could does the trick:
blog/(.+[a-zA-Z0-9])-+a[0-9]+ blog/$1
You can simplify [a-zA-Z0-9] removing all characters ranges that can't appear in the end of the articles name slug (ie [a-z0-9] or [a-z]).

RewriteRule: Allow only numbers and a specific string comma separated

I'm trying to rewrite urls to allow only numbers and (optionally) the word "all" comma separated.
For example, something like this: mypage/23,15,all,2,all
I tried something, but there is a problem. First, this is my rule (probably syntatically wrong):
mypage/([\d,?(all)]+)
The problem here is that if a write mypage/23 works (correct), mypage/23,all works (correct), mypage/23,all,a works (because it detects 'a' of 'all', so wrong)
How can I modify the rule to accept only the entire word "all"?
Thank you.
You can use this regex:
RewriteRule ^mypage/((?:\d+|all)(,(?:\d+|all))*)/?$ target-url?str=$1 [L,NC,QSA]
There is no grouping of characters inside character class i.e. [...]
Try this: i think it should work
/([\d]+,(all)+)
[] for optional
() for grouping

Regex for string that contains a '='

I've tried to create a regular expression that validates a string and checks if it has a = character in it.
I also need it to be in brackets like this
(.*)
in order to retrieve the value later.
What I tried was
(.*=.*)
but it doesn't work.
How can I match a string that contains a = ?
Edit:
This is my regex from my htaccess file:
RewriteRule ^(home|page1|page2|page3|admin)/(.*)/(.*)/(.*=.*) index.php?area=$1&page=$2&content=$3&$4 [L]
RewriteRule ^(home|page1|page2|page3|admin)/(.*)/(.*) index.php?area=$1&page=$2&content=$3 [L]
Examples would be
/home/foo/bar and /home/foo/bar/page=2
That's what I pretty much want to achieve. Add GET parameters in an eye-candy way. Also, I need to parse if it contains a = character, because there are various depths in the web site such as /foo/page=1 and foo/bar/page=1
Actually this works for me. This call:
preg_match('/.*=.*/','foo=bar');
returns 1.
However, if you just want to check if the string contains =, then strpos is just enough.
If, instead, it is in the context of a bigger regular expression, the problem may be elsewhere. Please show us the whole matching pattern and some sample inputs with the corresponding expected behaviour.