Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Given the following Apache rewrite rule:

RewriteRule .* foo

...how do I find out precisely what string .* is testing against? Assume that any number of arbitrary rewrite rules could have been applied before this point, and that I have no way of knowing what they are (assume httpd.conf is hidden from me).

EDIT: Thanks, though keep in mind that I do not have access to edit (or even read!) httpd.conf. I only have .htaccess files at my disposal.

share|improve this question
add comment

4 Answers

EDIT: removed solution that needed httpd.conf access

This will work, just put it on the line before your rewrite rule:

RewriteRule (.*) - [ENV=MYREWRITERULETEST:$1]

Then in a .ssi or .cgi file, output that variable.

Attempts to look at other variables won't necessarily tell you what your rule is matching against; they'll tell you what was matched against before any rewrite rules ran or what things look like after all the rewrite rules ran.

share|improve this answer
add comment

mod_rewrite uses regular expressions to identify a particular string and then rewrites that match with the replacement given. In the case of your example:

RewriteRule .* foo

mod_rewrite will match any character (that's what the "." stands for), as many times as it repeats (thus the "*"). Now, since "*" is repeating ".", this means that it is going to match any string with any characters of any combination and length. Then, it's going to replace that big, long any with "foo".

So to use mod_rewrite, you need to have at least a basic understanding of regular expressions, as well as a full understanding of what it is you want to match and rewrite it as.

I often use a small program like Reggy or Kiki to help me layout my regular expression. You may find this helpful as well.

I hope this helps.

share|improve this answer
    
I wasn't asking what kind of strings ".*" will match, I'm quite familiar with regular expressions ;-) I'm asking: what string is that pattern being tested against? %{REQUEST_URI}? %{SOMETHING_ELSE}? Etc. For any regular expression, you've got pattern and an input. I know the pattern; what's the input to RewriteRule? Or more precisely, how do I find out what the current input to RewriteRule is? –  dirtside Jun 23 '09 at 3:28
2  
Thank you for clarifying. mod_rewrite watches for two environment variables: SCRIPT_URL and SCRIPT_URI. The documentation for mod_rewrite (httpd.apache.org/docs/2.2/mod/mod_rewrite.html) has this all in excruciating detail. –  Shazburg Jun 23 '09 at 4:26
add comment

If you had access to httpd.conf, this answer describes how to turn on logging for mod_rewrite, which will give you exactly what you want.

If you have access to the .htaccess file in the file system being served, you can make the config changes there to activate logging.

Otherwise, Trial and error may be your best bet!

share|improve this answer
add comment

I know this is a header in IIS, I don't know about Apache, but the header UNENCODED_URL or HTTP_URL might give you the server-side URL if you query it in your server-side script (ASP/PHP/whatever).

You can then use that to back-track the rule in the .htaccess

share|improve this answer
    
Apache has an equivalent variable, I'm sure (don't remember what it is right now), but I don't think it's what the OP is looking for. –  David Z Jun 24 '09 at 18:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.