SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    htaccess Rewrite Rules... Then everything possible url remaining

    This question is difficult, but let me try. I am going to change the link structure on my website and was hoping to have my index file basically handle most of what needs to be done in order of discovering what page needs to be opened (or included) based on the URL supplied. For now I'm leaving my original rewrites with updated canonicals to point search engines to the new pages. So, to keep rewrites, while allowing the index.php to pick up everything else, would this work?

    Code:
    RewriteRule ^/?trailer/([-0-9a-zA-Z#]+)/([-0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?$  file1.php?fkey=$1&tkey=$2&tres=$3
    
    RewriteRule ^/?trailers/([0-9]+)$  file2.php?page=$1
    
    RewriteRule ^/?communities social.php
    
    RewriteRule ^/?user/([0-9]+)?/?([0-9a-zA-Z#]+)?/?([0-9]+)?  usr.php?uid=$1&pagetype=$2&page_number=$3
    
    //and so on and so on of rewrite rules... and then:
    
    RewriteRule ^/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?  index.php?var1=$1&var2=$2&var3=$3&var4=$4
    So basically if the URL doesn't fit any of the exact rewrites, then it checks it against the final rewrite url that has the index.php give it a look.

    Would this work or create an immediate conflict?

    Cheers!
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

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

    It should work except for:

    1. # is a reserved character (Uniform Resource Identifiers (URI): Generic Syntax) so it should be removed from your regex.

    2. You use a LOT of optional atoms so your scripts do need to handle null values.

    3. EVERYTHING in the final RewriteRule is optional so it will match everything (without dot characters). This SHOULD be okay given your presented code BUT, to be safe, I'd preface that with a RewriteCond {REQUEST_FILENAME} !-f but DEFINITELY check for directory, too (!-f). What you are doing is what WordPress has done for years.

    4. For what it's worth, when Apache 2 came out, it refused to match leading /'s while Apache 1 still required the / in the DocumentRoot. You're using the OPTIONAL leading / (acceptable to both) but I doubt that there are many Apache 1 servers left out there so I'd just (be lazy and) delete the /? in your regex throughout.

    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

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks DK!

    To be honest, I'm using nginx, so I was using the apache rewrites to simplify what I've been doing (before converting to nginx standard) and what I wanted to change. My assumption that the conversion to nginx format would work just the same.

    Or, wishful thinking?

    Cheers!
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So on the bottom of the rewrite rules we'd have:

    RewriteCond {REQUEST_FILENAME} !-f
    RewriteRule ^/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)? index.php?var1=$1&var2=$2&var3=$3&var4=$4
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which I think in nginx:

    location / {
    if (!-f $request_filename) {
    rewrite ^/?([0-9a-zA-Z]+)?/?([0-9a-zA-Z]+)?/?([0-9a-zA-Z]+)?/?([0-9a-zA-Z]+)?
    /index.php?var1=$1&var2=$2&var3=$3&var4=$4;
    }
    }
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

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

    Quote Originally Posted by casbboy View Post
    So on the bottom of the rewrite rules we'd have:

    Code:
    RewriteCond {REQUEST_FILENAME} !-f
    RewriteCond {REQUEST_FILENAME} !-d
    RewriteRule ^/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?/?([0-9a-zA-Z#]+)?  index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L]
    It's probably just as important to check the directory, too.

    Sorry, I can't comment on your translation to nginx except to wonder ... where their documentation is (NOT that I want to go look at it). I know that M$ has two versions of their attempt to emulate Apache's mod_rewrite (and only one uses similar syntax) without duplicating all mod_rewrite's features.

    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

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,760
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's definitely tricky, but looks like nginx combines the !-f and !-d into one, as demonstrated by WordPress and Drupal in the nginx settings. For example, this is the nginx conversion for WordPress:

    Code:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    that becomes:

    Code:
    location / {
      if (!-f $request_filename) {
        rewrite ^(.*)$ /index.php;
      }
    }
    I'm testing and will let you know. Right now my rewrites are conflicting with one another. Trying to smooth out.
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.


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
  •