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.

Hi I have written some rewrites, but from what I can tell from the logs it seems that the rules keep executing and so, although the correct result is achieved mid-execution, the final rewrite is incorrect.

Ideally I'd need to use the [END] flag but I can't upgrade the server software which is Apache 2.2, so I'm going to have to work around that.

I'm trying to rewrite "/wedding?url=emily-and-john-2013" to "/wedding/emily-and-john-2013".

As I'm using CodeIgniter I first need to rewrite "index.php/wedding/..." to "/wedding/...".

Here are the rules I have so far.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

    #Sends requests for /wedding/{blahblahblah} to
    #/wedding?url={blahblahblah}
    RewriteRule ^index.php/wedding/([^_]*)$ index.php/wedding?url=$1 [L]
</IfModule>

Here are the logs (slightly sanitised but you get the idea):

 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/wedding -> SRV_PATH/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/wedding/emily-and-john-2013 -> wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (3) [perdir SRV_PATH/] applying pattern '^(.*)$' to uri 'wedding/emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/wedding' pattern='!-f' => matched
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/wedding' pattern='!-d' => matched
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (2) [perdir SRV_PATH/] rewrite 'wedding/emily-and-john-2013' -> 'index.php/wedding/emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (3) [perdir SRV_PATH/] add per-dir prefix: index.php/wedding/emily-and-john-2013 -> SRV_PATH/index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (2) [perdir SRV_PATH/] trying to replace prefix SRV_PATH/ with /
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (5) strip matching prefix: SRV_PATH/index.php/wedding/emily-and-john-2013 -> index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (4) add subst prefix: index.php/wedding/emily-and-john-2013 -> /index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b93e1c68/initial] (1) [perdir SRV_PATH/] internal redirect with /index.php/wedding/emily-and-john-2013 [INTERNAL REDIRECT]
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/index.php -> SRV_PATH/index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/index.php/wedding/emily-and-john-2013 -> index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (3) [perdir SRV_PATH/] applying pattern '^(.*)$' to uri 'index.php/wedding/emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/index.php' pattern='!-f' => not-matched
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/index.php -> SRV_PATH/index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/index.php/wedding/emily-and-john-2013 -> index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (3) [perdir SRV_PATH/] applying pattern '^index.php/wedding/([^_]*)$' to uri 'index.php/wedding/emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (2) [perdir SRV_PATH/] rewrite 'index.php/wedding/emily-and-john-2013' -> 'index.php/wedding?url=emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (3) split uri=index.php/wedding?url=emily-and-john-2013 -> uri=index.php/wedding, args=url=emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (3) [perdir SRV_PATH/] add per-dir prefix: index.php/wedding -> SRV_PATH/index.php/wedding
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (2) [perdir SRV_PATH/] trying to replace prefix SRV_PATH/ with /
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (5) strip matching prefix: SRV_PATH/index.php/wedding -> index.php/wedding
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (4) add subst prefix: index.php/wedding -> /index.php/wedding
 [*/sid#b8f1e8f0][rid#b9174a78/initial/redir#1] (1) [perdir SRV_PATH/] internal redirect with /index.php/wedding [INTERNAL REDIRECT]
 [*/sid#b8f1e8f0][rid#b917e088/initial/redir#2] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/index.php -> SRV_PATH/index.php/wedding
 [*/sid#b8f1e8f0][rid#b917e088/initial/redir#2] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/index.php/wedding -> index.php/wedding
 [*/sid#b8f1e8f0][rid#b917e088/initial/redir#2] (3) [perdir SRV_PATH/] applying pattern '^(.*)$' to uri 'index.php/wedding'
 [*/sid#b8f1e8f0][rid#b917e088/initial/redir#2] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/index.php' pattern='!-f' => not-matched
 [*/sid#b8f1e8f0][rid#b917e088/initial/redir#2] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/index.php -> SRV_PATH/index.php/wedding
 [*/sid#b8f1e8f0][rid#b917e088/initial/redir#2] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/index.php/wedding -> index.php/wedding
 [*/sid#b8f1e8f0][rid#b917e088/initial/redir#2] (3) [perdir SRV_PATH/] applying pattern '^index.php/wedding/([^_]*)$' to uri 'index.php/wedding'
 [*/sid#b8f1e8f0][rid#b917e088/initial/redir#2] (1) [perdir SRV_PATH/] pass through SRV_PATH/index.php
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/wedding -> wedding
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (3) [perdir SRV_PATH/] applying pattern '^(.*)$' to uri 'wedding'
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/wedding' pattern='!-f' => matched
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/wedding' pattern='!-d' => matched
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (2) [perdir SRV_PATH/] rewrite 'wedding' -> 'index.php/wedding'
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (3) [perdir SRV_PATH/] add per-dir prefix: index.php/wedding -> SRV_PATH/index.php/wedding
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (2) [perdir SRV_PATH/] trying to replace prefix SRV_PATH/ with /
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (5) strip matching prefix: SRV_PATH/index.php/wedding -> index.php/wedding
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (4) add subst prefix: index.php/wedding -> /index.php/wedding
 [*/sid#b8f1e8f0][rid#b93a1c80/subreq] (1) [perdir SRV_PATH/] internal redirect with /index.php/wedding [INTERNAL REDIRECT]

Note that on line 19 the correct URL is shown! Unfortunately it keeps running after that and some how the end result isn't correct.

Any ideas of how to change the rules to achieve the desired effect?

EDIT

I've just noticed that if I set the [R] flag for a redirect rather than a rewrite then the browser ends up pointing at the correct URL. Not sure what bearing that has on anything.

EDIT 2

With the [NS] flag added to the index.php rule, here are the logs:

 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/wedding -> SRV_PATH/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/wedding/emily-and-john-2013 -> wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (3) [perdir SRV_PATH/] applying pattern '^(.*)$' to uri 'wedding/emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/wedding' pattern='!-f' => matched
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/wedding' pattern='!-d' => matched
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (2) [perdir SRV_PATH/] rewrite 'wedding/emily-and-john-2013' -> 'index.php/wedding/emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (3) [perdir SRV_PATH/] add per-dir prefix: index.php/wedding/emily-and-john-2013 -> SRV_PATH/index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (2) [perdir SRV_PATH/] trying to replace prefix SRV_PATH/ with /
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (5) strip matching prefix: SRV_PATH/index.php/wedding/emily-and-john-2013 -> index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (4) add subst prefix: index.php/wedding/emily-and-john-2013 -> /index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9174dd0/initial] (1) [perdir SRV_PATH/] internal redirect with /index.php/wedding/emily-and-john-2013 [INTERNAL REDIRECT]
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/index.php -> SRV_PATH/index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/index.php/wedding/emily-and-john-2013 -> index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (3) [perdir SRV_PATH/] applying pattern '^(.*)$' to uri 'index.php/wedding/emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/index.php' pattern='!-f' => not-matched
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/index.php -> SRV_PATH/index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/index.php/wedding/emily-and-john-2013 -> index.php/wedding/emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (3) [perdir SRV_PATH/] applying pattern '^index.php/wedding/([^_]*)$' to uri 'index.php/wedding/emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (2) [perdir SRV_PATH/] rewrite 'index.php/wedding/emily-and-john-2013' -> 'index.php/wedding?url=emily-and-john-2013'
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (3) split uri=index.php/wedding?url=emily-and-john-2013 -> uri=index.php/wedding, args=url=emily-and-john-2013
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (3) [perdir SRV_PATH/] add per-dir prefix: index.php/wedding -> SRV_PATH/index.php/wedding
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (2) [perdir SRV_PATH/] trying to replace prefix SRV_PATH/ with /
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (5) strip matching prefix: SRV_PATH/index.php/wedding -> index.php/wedding
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (4) add subst prefix: index.php/wedding -> /index.php/wedding
 [*/sid#b8f1e8f0][rid#b9157758/initial/redir#1] (1) [perdir SRV_PATH/] internal redirect with /index.php/wedding [INTERNAL REDIRECT]
 [*/sid#b8f1e8f0][rid#b9173fe8/initial/redir#2] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/index.php -> SRV_PATH/index.php/wedding
 [*/sid#b8f1e8f0][rid#b9173fe8/initial/redir#2] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/index.php/wedding -> index.php/wedding
 [*/sid#b8f1e8f0][rid#b9173fe8/initial/redir#2] (3) [perdir SRV_PATH/] applying pattern '^(.*)$' to uri 'index.php/wedding'
 [*/sid#b8f1e8f0][rid#b9173fe8/initial/redir#2] (4) [perdir SRV_PATH/] RewriteCond: input='SRV_PATH/index.php' pattern='!-f' => not-matched
 [*/sid#b8f1e8f0][rid#b9173fe8/initial/redir#2] (3) [perdir SRV_PATH/] add path info postfix: SRV_PATH/index.php -> SRV_PATH/index.php/wedding
 [*/sid#b8f1e8f0][rid#b9173fe8/initial/redir#2] (3) [perdir SRV_PATH/] strip per-dir prefix: SRV_PATH/index.php/wedding -> index.php/wedding
 [*/sid#b8f1e8f0][rid#b9173fe8/initial/redir#2] (3) [perdir SRV_PATH/] applying pattern '^index.php/wedding/([^_]*)$' to uri 'index.php/wedding'
 [*/sid#b8f1e8f0][rid#b9173fe8/initial/redir#2] (1) [perdir SRV_PATH/] pass through SRV_PATH/index.php
 [*/sid#b8f1e8f0][rid#b912c738/subreq] (1) [perdir SRV_PATH/] pass through SRV_PATH/wedding
share|improve this question

1 Answer 1

Can you move this rule out of the <Directory> or .htaccess context? The passthrough subrequest seems to be the problem.

If not, try adding an [NS] flag to the index.php rule to prevent it from acting on that subrequest?

RewriteRule ^(.*)$ index.php/$1 [NS,L]
share|improve this answer
    
I don't think I can move it out of the <Directory> or .htaccess context as this is a virtual host, and it's not relevant to the other virtual hosts. –  Josh Sep 25 '13 at 15:51
    
I've tried adding the [NS] flag and although the logs are cut down considerably (removing all of the subreq lines) the end result is the same. I'll add it to my OP. –  Josh Sep 25 '13 at 15:52
    
@Josh Where exactly are these rules configured now? Moving it to the <VirtualHost> context would achieve getting it out of the directory context. –  Shane Madden Sep 25 '13 at 16:07
    
@Josh Your new logs show that the last rewrite being applied sets the URL to uri=index.php/wedding, args=url=emily-and-john-2013, which is what you're going for if I understand you correctly? What are you seeing that makes you think it's not working correctly -- wrong content being returned by PHP, or wrong URL in the browser's address bar? –  Shane Madden Sep 25 '13 at 16:11
    
uri=index.php/wedding, args=url=emily-and-john-2013 is correct, unforunately the wrong content is being returned by PHP. However if I enter .../wedding?url=emily-and-john-2013 into the address bar then the correct content is returned, which leads to believe it's a rewrite issue rather than a PHP issue. –  Josh Sep 26 '13 at 7:48

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.