Solved

htaccess Rewrite Single Digit Wildcard

Posted on 2012-03-24
11
476 Views
Last Modified: 2012-03-29
Is there a single digit wildcard that can be used with RewriteRule?  Here's an example, this is my current code:

RewriteCond %{REQUEST_URI} .*user_screenshots/14.*
RewriteRule ^(.*)user_screenshots/14(.*)$ http://%{HTTP_HOST}/$1user_screenshots/saves14/14$2 [R,L]

If # was a single digit wildcard, this is what that code would look like:

RewriteCond %{REQUEST_URI} .*user_screenshots/14###.*
RewriteRule ^(.*)user_screenshots/14###(.*)$ http://%{HTTP_HOST}/$1user_screenshots/saves14/14###$2 [R,L]

So my question is, what is the single digit wildcard that can be used in this?

Thanks.
0
Comment
Question by:davideo7
  • 6
  • 5
11 Comments
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37761615
mod_rewrite accepts regular expressions.
if you want to match 0 or more characters use a single question mark (?).
If you want to match a digit use a character class like this:

[0-9]

(square brackets included)
0
 

Author Comment

by:davideo7
ID: 37761659
designatedinitializer: What if I want a wildcard of just 3 digits?
0
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 500 total points
ID: 37761745
that's 3 times the same formula:

[0-9][0-9][0-9]

Notice that this matches any 3 digits.
It won't match 2 digits, nor 1 digit, nor no digits at all.
0
 

Author Comment

by:davideo7
ID: 37761764
designatedinitializer: So would it look like this?

RewriteCond %{REQUEST_URI} .*user_screenshots/14[0-9][0-9][0-9].*
RewriteRule ^(.*)user_screenshots/14[0-9][0-9][0-9](.*)$ http://%{HTTP_HOST}/$1user_screenshots/saves14/14[0-9][0-9][0-9]$2 [R,L]
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37761830
yep

14[0-9][0-9][0-9]

 will match 14000 through 14999.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:davideo7
ID: 37761959
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37761981
I was talking about the wildcard/regexp you asked about.
What exactly are you trying to acomplish?
The example you give above shows two equal strings.
0
 

Author Comment

by:davideo7
ID: 37762016
0
 

Assisted Solution

by:davideo7
davideo7 earned 0 total points
ID: 37762033
Ok I figured it out, I had to rewrite my code like this:

RewriteCond %{REQUEST_URI} .*user_screenshots/14[0-9][0-9][0-9].*
RewriteRule ^(.*)user_screenshots/14([0-9])([0-9])([0-9])(.*)$ http://%{HTTP_HOST}/$1user_screenshots/saves14/14$2$3$4$5 [R,L]

Had to put brackets around the [0-9] and had to add $2$3$4 in there.
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37762946
Hi,
Yeah, you're right.
You could also do it like this: (14[0-9][0-9][0-9])/(.*)
And then use $1$2

The brackets turn the match pattern into a group.
The $n stuff refers to the n'ths group matched back there.
0
 

Author Closing Comment

by:davideo7
ID: 37780971
I was able to figure out the 2nd half of the problem on my own but designatedinitializer helped me with the first half.  Thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
This article discusses four methods for overlaying images in a container on a web page
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now