.htaccess RewriteRules help

Discussion in 'Web Design and Development' started by yagran, Sep 24, 2008.

  1. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #1
    Hi there i'm trying to achieve a rewrite so that i can link to pages on my website easier.

    here's what i currently have in a .htaccess file in the httpdocs file on my hosting:
    Code:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^page/home$ ?page=home.html
    RewriteRule ^page/blog$ ?page=blogcms/blog.php
    RewriteRule ^page/lineup$ ?page=lineup.html
    RewriteRule ^page/djs$ ?page=djs.html
    I cant really see whats wrong with it? but im such a novice at this that im not sure if its something in this file thats wrong, or if its something server setup related...?

    thanks for any advice
     
  2. mcnicks macrumors regular

    Joined:
    Jan 8, 2006
    #2
    Have a look at the RewriteBase directive. When you put RewriteRules in .htaccess files, you have to define the directory that the rules apply to.
     
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    Actually I don't think the RewriteBase part is needed if they are applying it to root, which I think is what he's doing, though could be wrong.

    To yagran, what URLs are you typing in to test these rewrite rules, and what are you expecting the URL to change to? Just want to make sure it's clear.
     
  4. yagran thread starter macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Try adding a [L] to the end of each line and/or remove the ^ on each line:
    Code:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^page/home$ ?page=home.html [L]
    RewriteRule ^page/blog$ ?page=blogcms/blog.php [L]
    RewriteRule ^page/lineup$ ?page=lineup.html [L]
    RewriteRule ^page/djs$ ?page=djs.html [L]
     
  6. yagran thread starter macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #6
    Thanks, but it still doesnt work :(
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    Well figured it out. You can't use .htaccess files because your server isn't Apache, it's Windows IIS, so you'll have to find a different solution that works with that.
     
  8. stndn macrumors member

    Joined:
    Oct 22, 2006
    Location:
    earth
    #8
    How did it not work?
    Did you get page not found, or your file simply didn't output the page name as defined by .htaccess? Actually, do you want it to load the contents of page=djs.html, or do you want the page to actually change to ?page=djs.html

    Here I'm assuming the former. If it's the later, add [ R ] for redirect and make the rules in the examples become [ R, L ].

    I tried the code you posted and it worked just fine. FYI, the ^ in the rewrite rule simply marks the beginning of URL (so ^page means anything that begins with page).

    As for suggestion by angelwatt, having the [ L ] at each rule means do not continue processing the rest of the rules on the first match.

    In any case, could you tell me what you get by going to http://radiofluff.co.uk/?page=djs.html ? By default, it usually opens up index.html, index.php, or any other index.* files, and not djs.html.

    If what you want to achieve is that for people who opens http://radiofluff.co.uk/page/djs should get the file djs.html instead, try this rule:

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^page/home$ home.html [L]
    RewriteRule ^page/blog$ blogcms/blog.php [L]
    RewriteRule ^page/lineup$ lineup.html [L]
    RewriteRule ^page/djs$ djs.html [L]
    If you want them to load a specific file (for example, index.php) and pass the page as a parameter to $_GET, simply attach index.php to it:

    RewriteRule ^page/home$ index.php?page=home.html [ L ]


    FYI, this is what I used for testing, saving the file as index.php:
    PHP:
    <?php
    if (isset ($_GET['page'])) { print "<p>page is {$_GET['page']}</p>"; }
    else { print 
    "<p>no page</p>"; }

    /* contents of .htaccess:

    Options +FollowSymlinks
    RewriteEngine on

    RewriteRule ^page/blah$ ?page=blah.html
    RewriteRule ^page/huh$ ?page=huh.html

    */
    ?>
    -stndn.


    Edit: Whoopss.. just read the post by angelwatt. I guess this post won't do much good, then...
     
  9. yagran thread starter macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #9
    I have the ISAPI equivilent of mod_rewrite installed....

    It should handle .htaccess files the same way apache would.
     
  10. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    Based on the documentation for ISAPI I think you need to place the code inside the httpd.ini file rather than creating a .htaccess file. Their documentation isn't exactly wonderful though, so I may be wrong.
     

Share This Page