Solved

htaccess Rewrite Single Digit Wildcard

Posted on 2012-03-24
11
479 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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…

840 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