Allow only one more parameter in url after certain string - .htaccess

I need to create a condition in htaccess to block all urls, if the first parameter matches a string 'abcde' and there are more then 2 parameters after that.
For example
example.com/someotherstring/123/more - allowed
example.com/abcde/123 - allowed
example.com/abcde - allowed
example.com/abcde/123/toomany/params - blocked
Could anyone kindly advice how to compose such a condition please?
Many thanks

Related

Use RegEx in Python to extract URL and optional query string from web server log data

Disclosure: very much a regex newbie, so I'm trying to tweak some example code I found which parses web server log data into named groups. The snippet of my modified regex thus far that deals with the URL and query string groups:
(?P<url>.+)(?P<querystr>\?.*)
This works just fine when the string against which it's applied actually does have a query string on the URL (each group gets the expected bit of the string) but fails to match if there is none. So I tried adding a '?' after the "querystr" group to indicate that it was optional, i.e. (?P<querystr>\?.*)? ... if there's no query string then it works as expected (nothing is extracted into querystr), but when there is one, it is still extracted as part of url rather than separately into querystr.
What's the best way to identify optional groups (assuming that's even the right approach in this case)? Thanks in advance.
You can use
^(?P<url>[^?]+)(?P<querystr>\?.*)?$
Details
^ - start of string
(?P<url>[^?]+) - Group "url": any one or more chars other than ?
(?P<querystr>\?.*)? - an optional Group "querystr": a ? char and then any zero or more chars other than line break chars as many as possible
$ - end of string.
See the regex demo.

mod_rewrite allow all characters and numbers and latters

([0-9a-zA-Z/+]+)
currently, I'm using this rule also I want + and % to be allowed in this rule how to do it? please help me
Your rule is already allowing +, you just need to add % into your regexp. It should be like that: ([0-9a-zA-Z/+%]+)

Python34 - creating a list with substring comparison

(noobie question )
I've used BeautifulSoup to scrape a list of hrefs. I've then assigned them to a List. I wish to extract a specific set of them to another List. There are two substrings I can use to differentiate the ones I want - they all start with "dsp" and they have something like "&tab=tabs-1" in them .
How can I transfer only those from MyList[] to MyNewList[]?
I'm used to having a where clause to work with and cant seem to find any reference - I'm assuming it must be pretty basic
something like this?
urls = ['http://dsp.more/&tab=tabs-1&more" ', 'http://no.mat.ch']
new_urls = [ url for url in urls if url.startswith('http://dsp') and '&tab=tabs-1' in url]
print(new_urls)

Issue with .htaccess rewrite when missing second variable

I've written this short piece of php code that requires 2 variables name and id, now the code itself works as intended and is not my problem, the problem is that I want to shorten the link to this file from 'http://www.mypage.org/folder/index.php?name=name&id=0' to 'http://www.mypage.org/folder/name;0', like so:
RewriteRule ^([a-zA-Z0-9]+);(.*)$ index.php?name=$1&id=$2
But if someone enters a link like 'http://www.mypage.org/folder/name' with out the ';' separator they get a 404 page.
Is there a way to write a sort of if statement that also checks for links with out the ';'?
The php page can handle a missing id by defaulting to '0' as well as a missing name.
Thanks in advance!
Make ;0 part or URL optional:
RewriteRule ^([a-zA-Z0-9]+)(?:;(.*))?$ index.php?name=$1&id=$2
or like this (if the above does not work in Apache)
RewriteRule ^([a-zA-Z0-9]+)(;(.*))?$ index.php?name=$1&id=$3

Make an optional Get variable with this htaccess

I have this rule in my .htaccess:
RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3
Which works great for this url:
http://somesite.com/po_systems/build_system/60/495C31/1
But now I need an optional 4th Get variable to this rule that will give me the $_GET variable step like this:
http://somesite.com/po_systems/build_system/60/495C31/1/2
$_GET['step'] // 2
But I also need the rule to work if there is no 4th Get variable. So basically I need both 3 and 4 Get variables to work, making the 4th optional.
I would write it with two separate rules:
RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3&step=$4 [S=1]
RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3
If there are 4 path components, the first rule will match, and skip the next rule ([S=1]). Otherwise the next rule will try to match.
#Ulrich Palha's solution probably also works, but the regular expression is getting complicated. It will pass an empty step= parameter if there's no 4th path component, which may be fine. My solution will pass no step parameter if there's no 4th path component. Either way should work.
try
RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/?([^/]*)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3&step=$4

Resources