[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

mod_rewrite again

Posted on 2004-10-12
7
Medium Priority
?
306 Views
Last Modified: 2012-05-05
In reference to http://oldlook.experts-exchange.com:8080/Web/Web_Servers/Apache/Q_21163692.html
The solution appeared correct, and did not give any errors, but now that I'm trying to implement it, it's not working.

The rule
RewriteRule ^topic/(.*)/(.*)$           topic.php?blog=$1&topic=$2              [L]
Is turning
/topic/arantius/tech/
into
topic.php?blog=arantius/tech/arantius&topic=tech/
where of course it should be
topic.php?blog=arantius&topic=tech

Which isn't quite cutting it! =)
0
Comment
Question by:arantius
[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
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:kalosi
ID: 12295266
The rule should look like

RewriteRule ^/topic/(.*)/(.*)/$           topic.php?blog=$1&topic=$2              [L]

david
0
 
LVL 18

Author Comment

by:arantius
ID: 12298157
The lack of non-greedy wildcard operators in 1.3 makes that rule not quite work in the big picture.  Below is a full set of rules that almost works in 1.3, the problem is the trailing slashes.  I want them to be optional, not required.  If I put the ? after the / that I used in 2, that matches the / inside the wildcard operator, breaking my site.

RewriteEngine On
RewriteBase /

RewriteRule ^news/archive/(.*)\.(.*)\.html$  news.php?do=archive&feedID=$1&start=$2  [L]
RewriteRule ^news/archive/(.*)\.html$        news.php?do=archive&feedID=$1           [L]
RewriteRule ^news/read/$                     news.php?do=read                        [L]
RewriteRule ^newarticle/(.*)/$               newarticle.php?blog=$1                  [L]
RewriteRule ^newarticle/$                    /                                       [L,R]
RewriteRule ^comment/(.*)/(.*)/$             comment.php?blog=$1&article=$2          [L]
RewriteRule ^comment/(.*)/$                  /blog/$1/                               [L,R]
RewriteRule ^comment/$                       /                                       [L,R]
RewriteRule ^article/(.*)/(.*)/$             article.php?blog=$1&article=$2          [L]
RewriteRule ^article/(.*)/$                  /blog/$1/                               [L,R]
RewriteRule ^article/$                       /                                       [L,R]
RewriteRule ^archive/(.*)/(.*)/$             /archive.php?blog=$1&start=$2           [L]
RewriteRule ^archive/(.*)/$                  /archive.php?blog=$1                    [L]
RewriteRule ^archive/$                       /                                       [L,R]
RewriteRule ^stumble/(.*)/(.*)/$             /stumble.php?blog=$1&start=$2           [L]
RewriteRule ^stumble/(.*)/$                  /stumble.php?blog=$1                    [L]
RewriteRule ^stumble/$                       /                                       [L,R]
RewriteRule ^games/game\.(.*)\.html$         /games/index.php?gameID=$1              [L]
RewriteRule ^games/game-(.+)/$               /games/index.php?gameID=$1              [L]
RewriteRule ^topic/(.+)/(.+)/$               topic.php?blog=$1&topic=$2              [L]
RewriteRule ^topic/(.+)/$                    /topic.php?blog=$1                      [L]
RewriteRule ^topic/$                         topic.php                               [L]
RewriteRule ^blog/(.+)/?$                    blog.php?blog=$1                        [L]
RewriteRule ^blog/?$                         /                                       [L,R]
0
 
LVL 4

Expert Comment

by:kalosi
ID: 12298200
Insert the foolowing rule in the beginning

RewriteRule ^(.*)/$ $1

this will remove the trailing slashes. Dont forget to remove the trailing slashes from the other rules.

david
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 18

Author Comment

by:arantius
ID: 12300485
It doesn't quite remove the trailing slashes, it causes major problems.

With just the rules:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ $1
RewriteRule ^topic/(.+)/(.+)$               topic.php?blog=$1&topic=$2              [L]
RewriteRule ^topic/(.+)$                    /topic.php?blog=$1                      [L]
RewriteRule ^topic$                         topic.php                               [L]

It sends /topic/arantius/ to /topic.php?blog=arantius&topic=arantius/
And /topic/arantius/general/ to /topic.php?blog=arantius/general/arantius&topic=general/

The log shows:

66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] add path-info postfix: /home/arantius/public_html/topic -> /home/arantius/public_html/topic/arantius/general/
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic/arantius/general/ -> topic/arantius/general/
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] applying pattern '^(.*)/$' to uri 'topic/arantius/general/'
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (2) [per-dir /home/arantius/public_html/] rewrite topic/arantius/general/ -> topic/arantius/general
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] add per-dir prefix: topic/arantius/general -> /home/arantius/public_html/topic/arantius/general
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] add path-info postfix: /home/arantius/public_html/topic/arantius/general -> /home/arantius/public_html/topic/arantius/general/arantius/general/
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic/arantius/general/arantius/general/ -> topic/arantius/general/arantius/general/
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] applying pattern '^topic/(.+)/(.+)$' to uri 'topic/arantius/general/arantius/general/'
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (2) [per-dir /home/arantius/public_html/] rewrite topic/arantius/general/arantius/general/ -> /topic.php?blog=arantius/general/arantius&topic=general/
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) split uri=/topic.php?blog=arantius/general/arantius&topic=general/ -> uri=/topic.php, args=blog=arantius/general/arantius&topic=general/
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (2) [per-dir /home/arantius/public_html/] trying to replace prefix /home/arantius/public_html/ with /
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (1) [per-dir /home/arantius/public_html/] internal redirect with /topic.php [INTERNAL REDIRECT]
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#8121174/initial/redir#1] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic.php -> topic.php
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#8121174/initial/redir#1] (3) [per-dir /home/arantius/public_html/] applying pattern '^(.*)/$' to uri 'topic.php'
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#8121174/initial/redir#1] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic.php -> topic.php
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#8121174/initial/redir#1] (3) [per-dir /home/arantius/public_html/] applying pattern '^topic/(.+)/(.+)$' to uri 'topic.php'
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#8121174/initial/redir#1] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic.php -> topic.php
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#8121174/initial/redir#1] (3) [per-dir /home/arantius/public_html/] applying pattern '^topic/(.+)$' to uri 'topic.php'
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#8121174/initial/redir#1] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic.php -> topic.php
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#8121174/initial/redir#1] (3) [per-dir /home/arantius/public_html/] applying pattern '^topic$' to uri 'topic.php'
66.199.132.173 - - [13/Oct/2004:13:34:12 -0400] [www.arantius.com/sid#81d4744][rid#8121174/initial/redir#1] (1) [per-dir /home/arantius/public_html/] pass through /home/arantius/public_html/topic.php


Without the rule you suggested, the log is:

66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] add path-info postfix: /home/arantius/public_html/topic -> /home/arantius/public_html/topic/arantius/general/
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic/arantius/general/ -> topic/arantius/general/
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) [per-dir /home/arantius/public_html/] applying pattern '^topic/(.+)/(.+)$' to uri 'topic/arantius/general/'
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (2) [per-dir /home/arantius/public_html/] rewrite topic/arantius/general/ -> /topic.php?blog=arantius&topic=general/
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (3) split uri=/topic.php?blog=arantius&topic=general/ -> uri=/topic.php, args=blog=arantius&topic=general/
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (2) [per-dir /home/arantius/public_html/] trying to replace prefix /home/arantius/public_html/ with /
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#812ffe4/initial] (1) [per-dir /home/arantius/public_html/] internal redirect with /topic.php [INTERNAL REDIRECT]
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#8120e14/initial/redir#1] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic.php -> topic.php
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#8120e14/initial/redir#1] (3) [per-dir /home/arantius/public_html/] applying pattern '^topic/(.+)/(.+)$' to uri 'topic.php'
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#8120e14/initial/redir#1] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic.php -> topic.php
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#8120e14/initial/redir#1] (3) [per-dir /home/arantius/public_html/] applying pattern '^topic/(.+)$' to uri 'topic.php'
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#8120e14/initial/redir#1] (3) [per-dir /home/arantius/public_html/] strip per-dir prefix: /home/arantius/public_html/topic.php -> topic.php
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#8120e14/initial/redir#1] (3) [per-dir /home/arantius/public_html/] applying pattern '^topic$' to uri 'topic.php'
66.199.132.173 - - [13/Oct/2004:13:36:00 -0400] [www.arantius.com/sid#81d4744][rid#8120e14/initial/redir#1] (1) [per-dir /home/arantius/public_html/] pass through /home/arantius/public_html/topic.php
0
 
LVL 4

Accepted Solution

by:
kalosi earned 1000 total points
ID: 12305937
Well the behaviour is strange because the first rule should have the [R] redirect flag ... this should fix the problem.

if not you can change your rules in the following manner

RewriteEngine On
RewriteBase /
RewriteRule ^topic/(.+)/(.+[^/])/?$               topic.php?blog=$1&topic=$2              [L]
RewriteRule ^topic/(.+[^/])/?$                    /topic.php?blog=$1                      [L]
RewriteRule ^topic$                         topic.php                               [L]

I have a test binary and it produced the desired output in both cases:

-- with trailing slash
[oracle@rhel oracle]$ ./regmatcher '^topic/(.+)/(.+[^/])/?$' topic/one/two/ 3
Subexpression 0 index [0-14]: 'topic/one/two/'
Subexpression 1 index [6-9]: 'one'
Subexpression 2 index [10-13]: 'two'
[oracle@rhel oracle]$
-- without the trailing slash
[oracle@rhel oracle]$ ./regmatcher '^topic/(.+)/(.+[^/])/?$' topic/one/two 3
Subexpression 0 index [0-13]: 'topic/one/two'
Subexpression 1 index [6-9]: 'one'
Subexpression 2 index [10-13]: 'two'
[oracle@rhel oracle]$
0
 
LVL 18

Author Comment

by:arantius
ID: 12308008
Ah yes, the pseudo-non-greedy character class inversion .. might well work.  I'll get back to you.
0
 
LVL 18

Author Comment

by:arantius
ID: 12308256
Everything finally works right, thank you!  For interested parties, the final set of rules ended up being:

ErrorDocument 404 /404

RewriteEngine On
RewriteBase /
RewriteRule ^news/archive/(.*)\.(.*)\.html$  /news.php?do=archive&feedID=$1&start=$2 [L]
RewriteRule ^news/archive/(.*)\.html$        /news.php?do=archive&feedID=$1          [L]
RewriteRule ^news/read/?$                    /news.php?do=read                       [L]
RewriteRule ^news/?$                         /news.php                               [L]
RewriteRule ^newarticle/(.*[^/])/?$          /newarticle.php?blog=$1                 [L]
RewriteRule ^comment/(.*[^/])/(.*[^/])/?$    /comment.php?blog=$1&article=$2         [L]
RewriteRule ^article/(.*[^/])/(.*[^/])/?$    /article.php?blog=$1&article=$2         [L]
RewriteRule ^archive/(.*[^/])/(.*[^/])/?$    /archive.php?blog=$1&start=$2           [L]
RewriteRule ^archive/(.*[^/])/?$             /archive.php?blog=$1                    [L]
RewriteRule ^stumble/(.*[^/])/(.*[^/])/?$    /stumble.php?blog=$1&start=$2           [L]
RewriteRule ^stumble/(.*[^/])/?$             /stumble.php?blog=$1                    [L]
RewriteRule ^games/game\.(.*)\.html$         /games/index.php?gameID=$1              [L]
RewriteRule ^games/game-(.*[^/])/?$          /games/index.php?gameID=$1              [L]
RewriteRule ^topic/(.*[^/])/(.*[^/])/?$      /topic.php?blog=$1&topic=$2             [L]
RewriteRule ^topic/(.*[^/])/?$               /topic.php?blog=$1                      [L]
RewriteRule ^topic/?$                        /topic.php                              [L]
RewriteRule ^blog/(.*[^/])/?$                /blog.php?blog=$1                       [L]
RewriteRule ^404$                            /                                       [L,R]

(And yeah, all this stuff would line up in a monospace font.)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

656 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