Solved

htaccess Rewrite Single Digit Wildcard

Posted on 2012-03-24
11
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

623 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