RewriteRule: Allow only numbers and a specific string comma separated - regex

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

Related

Rewrite from url with querystring to folder

I want rewrite url like this:
/files/b9f8d0b5e35248579953755b3677a59b.png?w=400&h=100&mode=crop
To:
/files/400/100/crop/b9f8d0b5e35248579953755b3677a59b.png
My rule like:
.AddRewrite(#"^files/(.*)?w=(\d+)&h=(\d+)&mode=(.*)$", "files/$2/$3/$4/$1", true)
But it's not working, how can i fix it? Many thanks!
Your regular expression starts with the ^ which makes the pattern only match when it starts with files/. Otherwise it looks pretty good. I've used [^?] as a character group that matches anything except the ?, and a similar character group for [^&].
AddRewrite(#"/files/([^?]+)\?w=(\d+)&h=(\d+)&mode=([^&]+)", "/files/$2/$3/$4/$1", true)
^ Tested on https://www.regexplanet.com/share/index.html?share=yyyyye98k3r
You might want to consider what will happen if the query parameter order changes.

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]).

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.

Regex to Replace Spaces in Two Conditions with Isapi ReWrite

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.

How do I write this URL in Django?

(r'^/(?P<the_param>[a-zA-z0-9_-]+)/$','myproject.myapp.views.myview'),
How can I change this so that "the_param" accepts a URL(encoded) as a parameter?
So, I want to pass a URL to it.
mydomain.com/http%3A//google.com
Edit: Should I remove the regex, like this...and then it would work?
(r'^/(?P[*]?)/?$','myproject.myapp.views.myview'),
Add % and . to the character class:
[a-zA-Z0-9_%.-]
Note: You don't need to escape special characters inside character classes because special characters lose their meaning inside character sets. The - if not to be used to specify a range should be escaped with a back slash or put at the beginning (for python 2.4.x , 2.5.x, 2.6.x) or at the end of the character set(python 2.7.x) hence something like [a-zA-Z0-9_%-.] will throw an error.
You'll at least need something like:
(r'^the/uri/is/(?P<uri_encoded>[a-zA-Z0-9~%\._-])$', 'project.app.view'),
That expression should match everything described here.
Note that your example should be mydomain.com/http%3A%2F%2Fgoogle.com (slashes should be encoded).
I think you can do it with:
(r'^(?P<url>[\w\.~_-]+)$', 'project.app.view')