Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

mod_rewrite [R]

Options
  • 24-10-2007 5:18pm
    #1
    Registered Users Posts: 3,401 ✭✭✭


    Hi Lads,
    If I use the [R] the page works fine, css sheets and images (in another folder) work no problem.
    But if i leave out the [R] the css sheet and images aren't loaded into the page?
    Any suggestions?
    Gary
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^([^/]+)/$ county.php?county=$1
    RewriteRule ^([^/]+)/([^/]+)/$ pub.php?county=$1&pub=$2
    


Comments

  • Registered Users Posts: 568 ✭✭✭phil


    Gary, in order for people to help you, you need to be more specific. RewriteRules work on the basis of matching URLs and you haven't given any examples of URLs that do or don't work.

    I can see what you're trying to do with the rules above (i.e. separate it by county and then pub) so that www.domain.com/dublin goes to the county and www.domain.com/dublin/messrs goes to messrs, but it's pure speculation as to why that isn't working the way you think it should.

    1. Turn logging on (RewriteLog and RewriteLogLevel)
    2. Show example URLs that work. Show example URLs that don't. "Include CSS files" is vague. Show the path. Visit the URLs directly.
    3. Show your logs.

    I think your rules are too open.

    If I were you, I'd make a closed set of allowed characters instead of just matching anything apart from a "/". Not matching periods (fullstops) means that any "proper" file with a file extension will be allowed through also.


  • Registered Users Posts: 3,401 ✭✭✭randombar


    Hi Sorry about that my brain is a bit fried:

    So using the following mod_rewrite:
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^([^/]+)/$ county.php?county=$1 [R]
    RewriteRule ^([^/]+)/([^/]+)/$ pub.php?county=$1&pub=$2
    

    i.e.
    the county page will work due to the [R]
    ex http://www.ratemypub.ie/dublin/

    the pub page won't work due to the lack of [R]
    ex http://www.ratemypub.ie/dublin/whelan's/

    I'm very new to the mod_rewrite so not sure how to turn on logging? and that's probably why my regular expressions arent great either! Could you suggest a better RE (considering "(" and "&") are used.

    Thanks for your help
    Gary


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    first [R] stands for redirect and you don't really need that unless...

    second - when use the / for Rewrite first set the base url for your website:
    <base href="http://www.website.com" />
    

    and use the full path to your files (css, images, etc).

    also you need to clean up the url as well as the (') will give you an error

    you can do that using preg_replace function available in php

    [php]
    preg_replace('#[^0-9a-zA-Z\-\_ ]+#','',"your_string_here"];
    //this will delete all characteres that are not letter,numbers, spaces or underscore
    [/php]


  • Registered Users Posts: 568 ✭✭✭phil


    are these rewrite rules going into a .htaccess file or into your main apache configuration?


  • Registered Users Posts: 3,401 ✭✭✭randombar


    Do you have to use the full address for the images and css?

    Would this be the norm in webdesign (Learned from scratch sorry)

    Also how do you set the base? Is the code
    RewriteBase http://www.ratemypub.ie
    

    phil wrote: »
    are these rewrite rules going into a .htaccess file or into your main apache configuration?

    They are going into the .htaccess file, hopefully it will build up my security!


  • Advertisement
  • Registered Users Posts: 568 ✭✭✭phil


    If you're using a .htaccess file, a RewriteBase is probably not needed. Apart from that, I can't see any real problems with the regular expressions.

    RewriteBase is only really needed when you're using an Alias or similar and you want to change the base. By default, the base is the directory where your apache configuration lives. Also, the RewriteBase takes a path, not a full URI (like http://www.ratemypub.ie).
    See http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritebase

    You should set absolute paths to your CSS / JS / images unless you have a good reason for doing otherwise.

    i.e.

    /include/css/style.css
    /include/js/blah.js
    /images/myimage.gif

    Phil.


  • Registered Users Posts: 3,401 ✭✭✭randombar


    Hi,

    Thanks I guessed that about the RewriteBase all right!

    by absolute paths do you mean

    /public_html/images/image.jpg
    or
    http://www.ratemypub.ie/images/image.jpg

    it's working with the second one anyways.

    Also I've been reading about a way that your php pages can only been accessed by the htaccess file to heighten security? is this true?

    Working on the php forms now, all in all this is a great piece of kit, thanks for all the advice lads!


  • Registered Users Posts: 3,401 ✭✭✭randombar


    So I've got most of it working now, have only one more error.

    My .htaccess contains the following
    RewriteRule ^([^/]+)/([^/]+)/adddedrate/$ pub.php?county=$1&pub=$2&message=1 [R]
    RewriteRule ^([^/]+)/([^/]+)/addedpics/$ pub.php?county=$1&pub=$2&message=2 [R]
    RewriteRule ^([^/]+)/([^/]+)/addedmap/$ pub.php?county=$1&pub=$2&message=3 [R]
    RewriteRule ^([^/]+)/([^/]+)/spam/$ pub.php?county=$1&pub=$2&message=4 [R]
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ $3.php?county=$1&pub=$2 [R]
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ $3.php?county=$1&pub=$2&id=$4 [R]
    

    without the [R] I'm getting the error "can't find the page addedrate.php" i.e. it's skipping the 1st rewrite rule and moving onto the fifth one.?

    Here's an example of the page working:

    If you add a rate to this pub
    http://www.ratemypub.ie/Offaly/The-Wolftrap/addrate/

    the end of the form redirects to:
    http://www.ratemypub.ie/offaly/The-Wolftrap/addedrate/


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    the first rule has 3 d's in the page name


  • Registered Users Posts: 3,401 ✭✭✭randombar


    I might actually cry!

    Edit:

    no wait I fixed that it's something else?
    RewriteRule ^([^/]+)/([^/]+)/addedrate/$ pub.php?county=$1&pub=$2&message=1 [R]
    RewriteRule ^([^/]+)/([^/]+)/addedpics/$ pub.php?county=$1&pub=$2&message=2 [R]
    RewriteRule ^([^/]+)/([^/]+)/addedmap/$ pub.php?county=$1&pub=$2&message=3 [R]
    RewriteRule ^([^/]+)/([^/]+)/spam/$ pub.php?county=$1&pub=$2&message=4 [R]
    
    


  • Advertisement
Advertisement