Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

htaccess Rewrite Single Digit Wildcard

Posted on 2012-03-24
11
Medium Priority
?
483 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 2000 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

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
 
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
 

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

824 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