Welcome to WebmasterWorld Guest from 54.196.144.100

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Mod rewrite works as external, but not internal

     

QuickMax

12:11 am on Jun 18, 2013 (gmt 0)



I'm running Apache version 2.0.64 and trying to do as described in the reference 2.0 doc: [httpd.apache.org...]
Under "Content Handling" -> "From Old to New (intern)" which says:

Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. Actually we want that users of the old URL even not recognize that the pages was renamed.

Their example:
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo\.html$ bar.html


My usage is very similar to the example:
RewriteEngine on
RewriteRule ^myfile\.php$ /../myfile2.php?100


This is an edit within a .htaccess file (at location of myfile.php) and aims to only provide an internal rewrite for one file.

This is working as an external redirect, regardless of any flags I use (which shouldn't be needed). So what I am seeing is, if a browser is pointed to:

http://www.example.com/somedir/myfile.php the browser gets redirected to
http://www.example.com/myfile2.php?100

So the redirect works, the correct content is displayed, but it's external, the user can see the actual URL.

So what is the problem?

  • Is my RewriteRule logic flawed? I've detailed out the regex below.
  • Should I be doing something different since this is in a .htaccess file in one sub-directory not being done on a per-server context?
  • Is there something I need to configure on the Apache server?
  • Am I wasting my time with 2.0.x? :)


    Regex:
    ^ = start anchor
    myfile = I want a file that has a name of "myfile"
    \. = after myfile, it should have a period.
    php = after myfile. there should be a php extension
    $ = end anchor, so nothing after php

    Thank you for any help.
  • lucy24

    12:51 am on Jun 18, 2013 (gmt 0)

    WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



    /../myfile2.php?100 


    What the bleep? The pattern is fine-- well, you know it's fine, the rule is semi-working-- but a target shouldn't look anything like that.

    Forget about RewriteBase. Give the full site-absolute URL: everything after example.com, beginning with / slash. This gives the same result no matter where the rule is located. Only the pattern is affected by directory structure.

    Even setting aside the /../ what is ?100 supposed to mean? A ? in a target means "dump the existing query, if any, and replace it with whatever comes after the question mark". But "100" by itself isn't a query.

    Oh, and where's the end of the rule? RewriteRules always need the [L] flag unless there is a clear and specific reason to omit it. (Here there isn't.)

    Finally: You said the rule works as an external redirect. Where, I hesitate to ask, is it redirecting to?

    phranque

    5:09 am on Jun 18, 2013 (gmt 0)

    WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



    welcome to WebmasterWorld, QuickMax!


    most likely you have a rule in another .htaccess, perhaps in the document root directory, or in the server config file, that is causing that external redirect.
    if you have access to the server config file, you could try using the rewrite logging facility to see what's happening in detail.


    But "100" by itself isn't a query.

    other than reserved/special characters the query string is arbitrary and does not have to be in the form of ...?param1=value&param2=value...

    the query string specified by QuickMax is equivalent to a single parameter named '100' with a null value.

    Dideved

    7:59 am on Jun 18, 2013 (gmt 0)



    My usage is very similar to the example:
    RewriteEngine on
    RewriteRule ^myfile\.php$ /../myfile2.php?100


    Turns out all your need to do is remove the leading slash from the substitution.

    RewriteRule ^myfile\.php$ ../myfile2.php?100

    Dideved

    8:05 am on Jun 18, 2013 (gmt 0)



    Oh, and where's the end of the rule? RewriteRules always need the [L] flag unless there is a clear and specific reason to omit it. (Here there isn't.)


    There's no such rule, nor is this even a widely accepted recommendation.

    phranque

    9:26 am on Jun 18, 2013 (gmt 0)

    WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



    Turns out all your need to do is remove the leading slash from the substitution.


    i'm confused why that Substitution (with the leading slash) would look like an Absolute URL, forcing the external redirect.


    RewriteRules always need the [L] flag unless...
    There's no such rule, nor is this even a widely accepted recommendation.


    it's either a requirement for the application, or it's not.

    Dideved

    9:38 am on Jun 18, 2013 (gmt 0)



    i'm confused why that Substitution (with the leading slash) would look like an Absolute URL, forcing the external redirect.


    I don't think it would, and when I tried it, it didn't. You were probably right before that there are other factors in play we haven't seen.

    Though, based on what we have seen, the internal rewrite he was trying to achieve seems to work simply by removing the leading slash. Which makes sense to me. He wants to rewrite back one directory from the current directory, not back one directory from the document root.

    QuickMax

    10:52 am on Jun 18, 2013 (gmt 0)



    Thank you so much for all the help! I now have this working- here is my analysis of the issue:


    I'm just getting started with apache and this is my first Rewrite, so to focus on the regex my first rule was:
    RewriteRule ^myfile\.php$ http://www.example.com/myfile2.php?100


    So this worked, but did act as an external redirect, which I suspected since I had a full URL that mod_rewrite would assume to be external.

    So for my next step I change it to how I reported:
    RewriteRule ^myfile\.php$ /../myfile2.php?100

    I read some misleading advice that said that targets should start with a leading "/", which makes sense 99% of the time, unless you are using relative paths- which I am. So I was operating under the false assumption that the target just needed to start with "/" as a rule. Thanks to lucy24's first response, I realized that was a problem.

    The ?100 part of myfile2.php?100 is odd, but that is how that script works, it's actually a vbulletin CMS script which uses that notation a lot.

    Anyway, with the malformed /../ I'm guessing mod_rewrite was just balking at the rule and ignoring the rewrite- but what I am also guessing is that the original rewrite that has the full URL was still in mod_rewrites rule processing list so it was still being applied- which made the /../ look like it was working, even though the original rule was doing the work. I'm not 100% sure on that but I changed my rewrite to:
    RewriteRule ^myfile\.php$ /../myfile2JUNK.php?100

    and it was still doing the same thing of going to myfile2.php?100, and not going to myfile2JUNK.php which obviously did not exist.


    So as Dideved wrote, I should just be able to drop the leading slash- which made it legal and processed but unfortunately it didn't play well with the vbulletin script and the ?100 was basically being ignored.

    I tired a dozen or so ways to get it to work but ultimately got this to do what I needed:
    RewriteRule ^myfile\.php$ http://www.example.com/myfile2.php?100 [P]

    Which is what I started with, but with the Proxy Throughput P flag, which makes it work as a dynamic mirror, per: [httpd.apache.org...]

    I'm guessing this isn't the most optimized solution since I'm doing a proxy to my own site :) but I don't understand why the vbulletin script doesn't honor the ?100 without a full URL. None-the-less, this fits my needs for now.

    As phranque suggest, I should enable the logging facility so I'm not working blind. I do have access to it but was hesitate to cause problems on the whole server.

    Again, thank you very much for the help- you made all the difference!

    phranque

    11:30 am on Jun 18, 2013 (gmt 0)

    WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



    have you tried the internal rewrite to the leading slash URL-path? (and no ../)