Forum Moderators: phranque

Message Too Old, No Replies

simple mod rewrite double redirects

         

pnkFloyd

6:35 pm on May 26, 2011 (gmt 0)

10+ Year Member



Hi,

The rule below is working as expected and redirects vol/v22 to issue.php?v=22. And it keeps showing
http://localhost/Site/vol/22/
address in a browser after redirect, with HTTP status=200:

RewriteRule ^vol/v(.*) issue.php?v=$1

But slightly modified rule works unexpected and redirects vol/v22 page to issue.php?v=22/ :

RewriteRule ^vol/(.*) issue.php?v=$1

In a browser it shows new address vol/22/?v=22 and HTTP status is:
301 Redirect to:
http://localhost/Site/vol/22/?v=22


Why this happens? I really want to use simpler form without additional v.
Please help me. I am new to mod_rewrite and regex

P.S. Web server is Apache v2.2.17 on WinXP (WAMP 2.1e)

phpJoeMo

7:03 pm on May 26, 2011 (gmt 0)

10+ Year Member



What RewriteBase is declared?

Where does issue.php reside?

pnkFloyd

7:11 pm on May 26, 2011 (gmt 0)

10+ Year Member



RewriteBase is not declared. Here is a full .htaccess file at www\Site (c:\wamp\www\Site):

RewriteEngine On
RewriteRule ^vol/(.*) issue.php?v=$1 [L]

File issue.php is in www\Site directory.

phpJoeMo

7:16 pm on May 26, 2011 (gmt 0)

10+ Year Member



I'm not sure if this will work, but have you tried this with backslashes due to windows?


RewriteRule ^vol\(.*) \issue.php?v=$1


Try it with forward slashes as well

pnkFloyd

7:25 pm on May 26, 2011 (gmt 0)

10+ Year Member



Tried with both - no luck...

g1smd

7:32 pm on May 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure external redirects are listed before internal rewrites.

Make sure every RewriteRule uses the [L] flag.

Rules which implement an external redirect should use the [R=301,L] flags and include the domain name in the redirect target.

pnkFloyd

7:48 pm on May 26, 2011 (gmt 0)

10+ Year Member



I have no more redirects - this is the only one.
Here is rewrite log for well working rule (with additional v):

[rid#289cc28/initial] (2) init rewrite engine with requested uri /Site/vol/v22
[rid#289cc28/initial] (1) pass through /Site/vol/v22
[rid#289cc28/initial] (3) [perdir C:/wamp/www/Site/] strip per-dir prefix: C:/wamp/www/Site/vol/v22 -> vol/v22
[rid#289cc28/initial] (3) [perdir C:/wamp/www/Site/] applying pattern '^vol/v(.*)' to uri 'vol/v22'
[rid#289cc28/initial] (2) [perdir C:/wamp/www/Site/] rewrite 'vol/v22' -> 'issue.php?v=22'
[rid#289cc28/initial] (3) split uri=issue.php?v=22 -> uri=issue.php, args=v=22
[rid#289cc28/initial] (3) [perdir C:/wamp/www/Site/] add per-dir prefix: issue.php -> C:/wamp/www/Site/issue.php
[rid#289cc28/initial] (2) [perdir C:/wamp/www/Site/] strip document_root prefix: C:/wamp/www/Site/issue.php -> /Site/issue.php
[rid#289cc28/initial] (1) [perdir C:/wamp/www/Site/] internal redirect with /Site/issue.php [INTERNAL REDIRECT]
[rid#294fc98/initial/redir#1] (2) init rewrite engine with requested uri /Site/issue.php
[rid#294fc98/initial/redir#1] (1) pass through /Site/issue.php
[rid#294fc98/initial/redir#1] (3) [perdir C:/wamp/www/Site/] strip per-dir prefix: C:/wamp/www/Site/issue.php -> issue.php
[rid#294fc98/initial/redir#1] (3) [perdir C:/wamp/www/Site/] applying pattern '^vol/v(.*)' to uri 'issue.php'
[rid#294fc98/initial/redir#1] (1) [perdir C:/wamp/www/Site/] pass through C:/wamp/www/Site/issue.php

And here is a rewrite log for the rule without v:

[rid#27e1878/initial] (2) init rewrite engine with requested uri /Site/vol/22
[rid#27e1878/initial] (1) pass through /Site/vol/22
[rid#27e1878/initial] (3) [perdir C:/wamp/www/Site/] strip per-dir prefix: C:/wamp/www/Site/vol/22 -> vol/22
[rid#27e1878/initial] (3) [perdir C:/wamp/www/Site/] applying pattern '^vol/(.*)' to uri 'vol/22'
[rid#27e1878/initial] (2) [perdir C:/wamp/www/Site/] rewrite 'vol/22' -> 'issue.php?v=22'
[rid#27e1878/initial] (3) split uri=issue.php?v=22 -> uri=issue.php, args=v=22
[rid#27e1878/initial] (3) [perdir C:/wamp/www/Site/] add per-dir prefix: issue.php -> C:/wamp/www/Site/issue.php
[rid#27e1878/initial] (2) [perdir C:/wamp/www/Site/] strip document_root prefix: C:/wamp/www/Site/issue.php -> /Site/issue.php
[rid#27e1878/initial] (1) [perdir C:/wamp/www/Site/] internal redirect with /Site/issue.php [INTERNAL REDIRECT]
[rid#27e3880/initial] (2) init rewrite engine with requested uri /Site/vol/22/
[rid#27e3880/initial] (1) pass through /Site/vol/22/
[rid#27e3880/initial] (3) [perdir C:/wamp/www/Site/] strip per-dir prefix: C:/wamp/www/Site/vol/22/ -> vol/22/
[rid#27e3880/initial] (3) [perdir C:/wamp/www/Site/] applying pattern '^vol/(.*)' to uri 'vol/22/'
[rid#27e3880/initial] (2) [perdir C:/wamp/www/Site/] rewrite 'vol/22/' -> 'issue.php?v=22/'
[rid#27e3880/initial] (3) split uri=issue.php?v=22/ -> uri=issue.php, args=v=22/
[rid#27e3880/initial] (3) [perdir C:/wamp/www/Site/] add per-dir prefix: issue.php -> C:/wamp/www/Site/issue.php
[rid#27e3880/initial] (2) [perdir C:/wamp/www/Site/] strip document_root prefix: C:/wamp/www/Site/issue.php -> /Site/issue.php
[rid#27e3880/initial] (1) [perdir C:/wamp/www/Site/] internal redirect with /Site/issue.php [INTERNAL REDIRECT]
[rid#27e8b50/initial/redir#1] (2) init rewrite engine with requested uri /Site/issue.php
[rid#27e8b50/initial/redir#1] (1) pass through /Site/issue.php
[rid#27e8b50/initial/redir#1] (3) [perdir C:/wamp/www/Site/] strip per-dir prefix: C:/wamp/www/Site/issue.php -> issue.php
[rid#27e8b50/initial/redir#1] (3) [perdir C:/wamp/www/Site/] applying pattern '^vol/(.*)' to uri 'issue.php'
[rid#27e8b50/initial/redir#1] (1) [perdir C:/wamp/www/Site/] pass through C:/wamp/www/Site/issue.php

phpJoeMo

8:10 pm on May 26, 2011 (gmt 0)

10+ Year Member



What about this:

RewriteRule ^vol/([^/]*) /issue.php?v=$1 [L]

I think your problem is that (.*) matches forward slashes as well and adds them to your query.

[edited by: phpJoeMo at 8:11 pm (utc) on May 26, 2011]

pnkFloyd

8:11 pm on May 26, 2011 (gmt 0)

10+ Year Member



I checked issue.php - on first case it gets parameter v=22 and with the second one it gets v=22/

pnkFloyd

8:33 pm on May 26, 2011 (gmt 0)

10+ Year Member



phpJoeMo - your suggestion is working (it removes slash from query) but it still gets 301 status and a new url in a browser. And I can't understand why additional v in pattern makes so big difference.

phpJoeMo

9:10 pm on May 26, 2011 (gmt 0)

10+ Year Member



Sorry, I think I should've added a forward slash. Thus:

RewriteRule ^/vol/([^/]*) /issue.php?v=$1 [L]

Do you have any other rules after or before this?

g1smd

9:13 pm on May 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The leading slash is needed only "httpd.conf".

It must be omitted in ".htaccess".

You should end anchor the pattern and the * might need to be + here.

pnkFloyd

11:19 am on May 27, 2011 (gmt 0)

10+ Year Member



I gave up with previous pattern and found the way it should be (I excluded vol from the beginning):

RewriteRule ^([0-9]{1,3}[a-z]?)$ issue.php?v=$1 [L]

Now it works just how I needed. And here is the more advanced version with more parameters:

RewriteRule ^([0-9]{1,3}[a-z]?)(?:/(lt|en))?(?:/contrib/([a-z]+))?(?:/review/([a-z]+))?(?:/article/([a-z]+))?$ issue.php?v=$1&l=$2&c=$3&r=$4&a=$5 [L]

All redirects now are working as expected:

22 -> issue.php?v=22
22/en -> issue.php?v=22&l=en
22/review/abcd -> issue.php?v=22&r=abcd
22/contrib/abc -> issue.php?v=22&c=abc
22/en/article/abcd -> issue.php?v=22&l=en&a=abcd

I also noticed that previous problem was related to my site's directory structure:
Site/
    vol/
       20
       20a
       21
       22
       22a
       22b

So, when I entered vol/20 it redirected to vol/20/?v=20 and executed index.php(from vol/20 directory) with v=20 parameter.
But if I entered vol/18 it redirected to issue.php?v=18 because vol/18 directory does not exist.
So I decided to exclude vol from pattern. I still don't understand why it was redirecting to index.php. I guess this is default web servers behavior. And I would like to know if there is a way to avoid it.