LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 04-29-2013, 12:52 PM   #1
Zippy1970
Member
 
Registered: Sep 2007
Posts: 118

Rep: Reputation: 17
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?
 
Old 04-29-2013, 01:14 PM   #2
Sigg3.net
Member
 
Registered: Mar 2008
Location: Oslo, Norway
Distribution: Fedora 17, Ubuntu 12 LTS and Ubuntu server 10.04
Posts: 161

Rep: Reputation: 23
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

Last edited by Sigg3.net; 04-29-2013 at 01:18 PM. Reason: I was terribly wrong
 
Old 04-29-2013, 01:51 PM   #3
Zippy1970
Member
 
Registered: Sep 2007
Posts: 118

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by Sigg3.net View Post
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.
 
Old 04-29-2013, 02:10 PM   #4
Sigg3.net
Member
 
Registered: Mar 2008
Location: Oslo, Norway
Distribution: Fedora 17, Ubuntu 12 LTS and Ubuntu server 10.04
Posts: 161

Rep: Reputation: 23
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

Last edited by Sigg3.net; 04-29-2013 at 02:19 PM.
 
Old 04-30-2013, 06:10 AM   #5
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,955

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
Quote:
Originally Posted by Zippy1970 View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] mod_rewrite help! sriks Linux - Server 2 08-05-2010 03:50 PM
mod_rewrite delmoras Linux - Newbie 1 01-12-2009 12:16 PM
mod_rewrite basarab1310 Linux - Newbie 1 11-06-2008 08:57 AM
Help with mod_rewrite MicahCarrick Linux - Software 3 05-23-2007 09:09 PM
mod_rewrite mr_dizzle Linux - General 1 02-02-2006 08:32 PM


All times are GMT -5. The time now is 04:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration