SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    htaccess mod rewrite help

    Hey everyone,
    I have this in my htaccess file. It helps me convert my pages from page.php?id=4 to page.php/page4

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ./home/path/mysite.com/template.php [L]

    It works fine,

    But when i need this to work in other directories it doesn't.
    like:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ./home/path/mysite.com/folder1/folder2/template.php [L]

    Any help would be great. Thanks!

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got this to accomplish what I want, I would love some feed back though as to way the first code I posted didn't work.

    Options +FollowSymLinks
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ template.php [L,QSA]

  3. #3
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    In your first post, your rewrite rules... don't have any rules. Just urls. ??

    In your second post, you're sending everything to template.php. That makes no sense. How can every single page on your site be "template.php?"?

    And yes, you're sending EVERYTHING to template.php with the (.*) (means everything and nothing). Prolly looks like it works because beforehand you have the rewriteCond checking for not file or directory.

    Give us a full url (with example.com for domain name) of what you want the links to look like
    and another full url of the actual, real url of the file you want users to reach.

    You want your rewrite rules to be as specific as possible and still work. Yours now is much too general and in danger of looping.

    ^(.*)$ matched "template.php" you know.


    But I'll give it a try because I'm still so fresh and green that it's still thrilling to try : )
    If it was only
    page.php?id=4 to page.php/page4
    then it would be
    RewriteRule ^page\.php\/page([\d]+)$ page.php?id=$1 [L]

    I think.

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Sp,

    Correct - no regex in the RewriteRules AND the links were to PHYSICAL files (they MUST be to website files, i.e., accessible via http protocol)!

    Sending EVERYTHING (except files and directories which exist) to ONE script is EXACTLY what WordPress does. In other words, not only does it make sense (with the file and directory exclusions), it WORKS! That's why, with the file exclusion, it's okay for (.*) to match template.php as the NOT file exclusion is already false (prevents looping).

    Quote Originally Posted by Sp
    page.php?id=4 to page.php/page4
    then it would be
    Code:
    RewriteRule ^page\.php\/page([\d]+)$ page.php?id=$1 [L]
    A couple of problems with this:

    1. For the OP to use a filename in place of a directory, Options MultiViews must be enabled. That's an UGLY thing (and makes the redirect unnecessary if page.php can read the "filename" part of the URI - which it can).

    2. Do NOT escape the / in the regex.

    Otherwise, fine. Personally, I'd use page/{pageid} to keep to what the OP asked for OR merely use {page_title} and redirect that to the page script changing the id to the title key (and search the db for title rather than id) - more about that in my signature's tutorial with an example at http://wilderness-wally.com where almost ALL his links are handled by a script which fetches the articles by title.

    js,

    Your first code didn't work for the reasons specified above (no regex and physical links vs http links).
    Code:
    Options +FollowSymLinks 
    RewriteEngine On 
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ template.php [L,QSA]
    Well, that's the typical WP mod_rewrite except for the useless QSA (the query string is not affected by the redirection so the QSA flag is not needed). However, please note that it's left to template.php to parse the {REQUEST_FILENAME} to determine how to handle the request.

    A pedantic complaint of mine, though, is that, if you're NOT going to use $1, DON'T create it with the EVERYTHING atom when .? will work just as well (and faster).
    Code:
    RewriteRule .? template.php [L,QSA]
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,283
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    Sending EVERYTHING (except files and directories which exist) to ONE script is EXACTLY what WordPress does. In other words, not only does it make sense (with the file and directory exclusions), it WORKS! That's why, with the file exclusion, it's okay for (.*) to match template.php as the NOT file exclusion is already false (prevents looping).
    Is this because of the particular script in the back of WordPress or can anyone use this to send anything not a file or dir to a script with .?/.* without worrying about looping?

    Also I'm confused about multiviews-- if the file you're looking for doesn't exist, apache looks for anything starting with the name you asked for... (though I remember by husband saying he used it to ask for different language-versions of pages)
    For the OP to use a filename in place of a directory, Options MultiViews must be enabled.
    do you mean because if page.php is a file then page.php/page4 is something Apache has to figure out because page.php isn't a dir?

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Sp,
    Quote Originally Posted by Stomme poes View Post
    Is this because of the particular script in the back of WordPress or can anyone use this to send anything not a file or dir to a script with .?/.* without worrying about looping?
    Good question. It's because of the !-f and !-d (does not exist as a file or directory) is used to prevent looping (WP's index.php is the target of the redirection). As for .?/.*, WP uses (.*) but only needs .? as it ignores the {REQUEST_URI} variable - it reads the {REQUEST_FILENAME} and fetches necessary modules (to handle the request) based on its contents.
    Also I'm confused about multiviews-- if the file you're looking for doesn't exist, apache looks for anything starting with the name you asked for... (though I remember by husband saying he used it to ask for different language-versions of pages)
    MultiViews is used to fetch the page script when it's used in the middle of a URI (in the place normally reserved for a directory). To my knowledge (limited because I won't use this "thing"), it will not cause Apache to wonder off looking for something else to match/serve (to look for mod_negotiation - I don't fully understand that either but matching the language specified by the browser is a primary purpose).
    do you mean because if page.php is a file then page.php/page4 is something Apache has to figure out because page.php isn't a dir?
    [indent]Try it out! If you DON'T have MultiViews enabled, then page.php IS a request for a directory and the result should be a 404. With MultiViews, page.php is served (and it's up to page.php to parse the {REQUEST_FILENAME} for the "filename" to know what to do with the request.
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •