LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   mod_rewrite: Please explain this to me... (https://www.linuxquestions.org/questions/linux-server-73/mod_rewrite-please-explain-this-to-me-4175460023/)

Zippy1970 04-29-2013 11:52 AM

mod_rewrite: Please explain this to me...
 
I've been using mod_rewrite forever and thought I fully understood how it works. But apparently, I don't.

I want to do something very simple. I want to rewrite a URL such as this:

Code:

http://www.somedomain.com/pages/A1234
to this:

Code:

http://www.somedomain.com/cgi-bin/showpage.cgi?page=A1234
I figured this would do it:

Code:

RewriteCond %{REQUEST_URI} ^/pages/[a-zA-Z0-9]+$
RewriteRule ^/pages/(.*)$        /cgi-bin/showpage.cgi\?page=$1

Oddly enough, this does not work. But this does:

Code:

RewriteCond %{REQUEST_URI} ^/pages/[a-zA-Z0-9]+$
RewriteRule ^(.*)$        /cgi-bin/showpage.cgi\?page=$1

I really don't get why the first method doesn't work but the second does. Could somebody please explain that to me?

Sigg3.net 04-29-2013 12:14 PM

Perhaps it has something to do with how the regexp is parsed? I stared blankly at this for 10 minutes:
http://httpd.apache.org/docs/2.4/rew...tro.html#regex
Also:
Quote:

The Substitution can also contain back-references to parts of the incoming URL-path matched by the Pattern. Consider the following:

RewriteRule ^/product/(.*)/view$ /var/web/productdb/$1

The variable $1 will be replaced with whatever text was matched by the expression inside the parenthesis in the Pattern. For example, a request for http://example.com/product/r14df/view will be mapped to the path /var/web/productdb/r14df.
So I can understand what you're thinking. But this:
Code:

RewriteRule ^/pages/(.*)$        /cgi-bin/showpage.cgi\?page=$1
Does not show where (.*) ends as in the example above.

I think this image explains the logics, or sequence of parsing: http://httpd.apache.org/docs/2.4/ima...references.png

Zippy1970 04-29-2013 12:51 PM

Quote:

Originally Posted by Sigg3.net (Post 4941269)
But this:
Code:

RewriteRule ^/pages/(.*)$        /cgi-bin/showpage.cgi\?page=$1
Does not show where (.*) ends as in the example above.

Actually, yes it does. It should match anything up to the end of the line ($). So it should work, yet it doesn't.

Sigg3.net 04-29-2013 01:10 PM

In the example, view$ is used. That's why I thought (.*)$ was ambivalent.

Anyway, your 1st attempt seems at a first glance to set the pattern to /pages/pages/whatever and not /pages/whatever. The condition is ALREADY that you're getting requests to /pages/whatever.. I'm not sure exactly for what reason it fails, but it is easy to see that it is incorrect. The pattern in Rewriterule doesn't match anything. That is,
Code:

RewriteRule ^/pages/(.*)$        /cgi-bin/showpage.cgi\?page=$1
will re-write requests to www.somedomain.com/pages/pages/1234 because the pattern that the rule is applied to is /pages/(.*) AFTER the /pages/ condition is met. So the rule will probably work for /pages/pages/1234

bathory 04-30-2013 05:10 AM

Quote:

Originally Posted by Zippy1970 (Post 4941289)
Actually, yes it does. It should match anything up to the end of the line ($). So it should work, yet it doesn't.

You need to remove the leading slash of the URI like this:
Code:

RewriteRule ^pages/(.*)$  /cgi-bin/showpage.cgi\?page=$1


All times are GMT -5. The time now is 02:28 PM.