RewriteRule Detecting numbers greater than 9 - .htaccess

I'm trying to detect numbers in a URL greater than 9 at the end of my URL. For example:
"...com/celtic-jewelry/traditional/10"
I've a work around in my .htaccess using two seperate lines that work as a combination (the 1st line detecting number less than 10, the 2nd detecting numbers over 9, but I'm wondering what the correct method is? My .htaccess is below, thanks!
RewriteRule ^celtic-jewelry/([a-z-'/]+)/([0-9])$ /product.php?flags=$1&page=$2 [L]
RewriteRule ^celtic-jewelry/([a-z-'/]+)/(.[0-9])$ /product.php?flags=$1&page=$2 [L]

If I got it right:
RewriteRule ^celtic-jewelry/([a-z-'/]+)/([1-9][0-9]+)$ product.php?flags=$1&page=$2 [L]

How about just the following single rule, to replace your two rules:
RewriteRule ^celtic-jewelry/([a-z'/-]+)/([0-9]+)$ /product.php?flags=$1&page=$2 [L]
Here [0-9]+ means "one or more digits 0-9". So that catches all numbers of 1, 2, or more digits, in just one rule. Although the title of your question is about numbers greater than 9, the context makes it clear that you're trying to capture all numbers, whether 1, 2, or more digits.
Also, note that in [a-z'/-]+, I moved the second - to the end of the brackets. You wrote it as [a-z-'/]+, but the problem there is that the second - could act as a range operator, while I think your intent was to use it as a literal dash character. To keep the - from acting as a range operator, you have to either put it first or last within the brackets, or else escape it with a backslash (\-).

Related

What do you understand by this RegEx?

I´m working with VBA and trying to split a string into three columns, almost all strings are like Company Name 3567782 Agent Name.pdf
With this pattern I want to match all the text before a space and digits (1st group), the digits (2nd group) and all the text after the space and before the .pdf (3rd group).
strPattern = "^(.+)\n(\d{4,10})\n(.+).pdf"
I recall spaces in python are \s but saw in VBA are \n.
Can you help me find the right pattern for what I´m looking for?
As I put in my comment, I use the https://regex101.com site. There are others but I find this one the most helpful to me.
When I put in your regex
^(.+)\n(\d{4,10})\n(.+).pdf
and test string
Company Name 3567782 Agent Name.pdf
the first thing I notice is that the regex does not match the test string (see right side under MATCH INFORMATION).
Here are a couple things that I saw:
\n is newline, not space. In regex, space is " ".
Your last "." in ".pdf" is not registering as a literal period, it's a token that matches any character. To match a literal period, you need \.
If we change those two things it returns three groups that seem to match what you are looking for.
^(.+) (\d{4,10}) (.+)\.pdf
It looks like for the digits, you are looking for between 4 and 10 digits. If that's correct, it looks like your regex is good. You could put in a handful of example strings into the TEST STRING area and make sure that it works in all cases.
I'd use either of these:
(?:(?:([a-zA-Z]+\.?)|(\d+)))
capture a-Z greedy with a possible . to allow for the .pdf or capture digits
this version excludes the space [ ] or \s
or keep the search structured so you can control what goes in and out of each column
^(\w+\s\w+)|(\d+)|(\w+\s\w+\.\w+$)
\b or ^ - word boundary or start of string
(\w+\s\w+) - 1st capture \w+ - any alpha numeric char greedily, followed by 1 x space (use \s* or \s+ for more), followed again by alpha numeric greedily
|(\d+) - alteration - \d+ - capture just digits
`|(\w+\s\w+.\w+$) - similar to 1st group but allows for the '.' of pdf and bounds to the end of string (\G or $).
you could optionally build the '.' into the 1st group like my top answer, but for neatness and better control I prefer the 2nd.

Subdomain to Subdirectory Formula in Excel

I am trying to convert 'http://city.example.com' to 'http://www.example.com/city' in an Excel sheet.
I have tried a multi-step approach to first TRIM or SUBSTITUTE the prefix then the suffix, but I'm in over my head.
Any help would be greatly appreciated. Thanks in advance!
Do heed the comment regarding formulating a proper question and posting what you've tried and what is going wrong.
Either way, assuming your domains are in column A and there is only a single subdomain:
=SUBSTITUTE(A1,MID(A1,FIND("//",A1,1)+2,FIND(".",A1,1)-(FIND("//",A1,1)+2)),"www")&MID(A1,FIND("//",A1,1)+2,FIND(".",A1,1)-(FIND("//",A1,1)+2))
Breaking it down into pieces:
FIND("//",A1,1)+2 will return the index of the start of the subdomain.
FIND(".",A1,1) will return the index of the first period, the end of the subdomain.
As such, the string from the start of the subdomain to the period will be the subdomain - since we're getting index's back we have to subtract them to get our MID length - FIND(".",A1,1)-(FIND("//",A1,1)+2)
Chaining those together, we can say we want to substitute whatever is from the // to the first period with www. - SUBSTITUTE(A1,MID(A1,FIND("//",A1,1)+2,FIND(".",A1,1)-(FIND("//",A1,1)+2)),"www")
This now leaves us with the subdomain replaced with www, so we just need to append the subdomain now, which we already know how to get - it's the mid formula we just used:
MID(A1,FIND("//",A1,1)+2,FIND(".",A1,1)-(FIND("//",A1,1)+2)
As such, we concatenate our two formulas together to get the result.

.htaccess Rewrite Rule based on numeric value

I'd like to do a rewrite-rule at htaccess. The following URL should be rewrited:
http://www.website.com/folder/subfolder/subsub/product-p00000
I'd like to make a rewrite-URL that recognizes the "-p" and then a numeric value. How do I make this rule? I have the following, but not sure how to add the numeric value to it:
RewriteRule ^(.*)/(.*)/(.*)/(.*)-p(.*)/?$ /file.php?folder=$1&subfolder=$2&subsub=$3&product=$4&id=$5
However, in the case of the following URL this goes wrong ofcourse:
http://www.website.com/folder/subfolder/subsub/product-product
How do I add the condition that the URL should end on "-p" followed by a numeric value?

Apache htaccess rewrite rule subtract a number

For some reason I have to subtract a number from $1 here, which will be a digit:
RewriteRule ^categories\/([0-9]+)\/(.*)?$ /at.php?-category_$1 [L,NC]
I want to pass $1-4 into category number here. For example, if URL is:
http://localhost/categories/12/xyz
Category id will be 12-4 which will be 8.
Can we do some simple plus minus operations here?

Extract Text from URL in Excel

I'd like to do the following in Excel:
http://www.examplesite.com/ABCD123.php --> /ABCD123.php
http://www.examplesite.com/folder/EFG567.php --> /folder/EFG567.php
Any help will be highly appreciated.
This is more generic and is based on assuming that .com/ will always be in a web address (though clearly that assumption is not robust when one considers .co.uk etc.)
=RIGHT(A1,LEN(A1)-(FIND(".com/",A1,1) + 4))
Actually figured it out:
=RIGHT(H8,LEN(H8)-26)
where 26 is the first 26 characters (http://www.examplesite.com).
There must be a more elegant/general solution for this though (i.e. Finding the number of characters before the ".com/")
This is quite hacky as it assumes that the address will always start with http://
=MID(A1,FIND("/",A1,8),LEN(A1)+1-FIND("/",A1,8))
Translated:
character position 8 is the first position after the http:// part
starting from pos 8, find the position of the first occurence of "/"
now subtract that position from the overall length of the string and add 1 to avoid losing the final character
now extract the substring from the position of that first "/" extending for the number of characters that we just calculated

Resources