SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Escaping static home pages from sitewide rewrite rule

    Imagine a site with just three static pages - a home page and home pages for About and Introduction sections...

    index.php
    (Folder About) index.php
    (Folder Topics) index.php

    Now imagine an .htaccess file with rewrite rules that allow me to display dynamic pages in the About and Topics sections. However, a third rewrite rule allows me to display dynamic pages on the website's home page. This is what it looks like:

    Code:
    RewriteRule ^About/([a-zA-Z0-9()_/-]+)/?$ About/index.php?bout=$1 [L]
    RewriteRule ^Topics/([a-zA-Z0-9()_/-]+)/?$ Topics/index.php?topic=$1 [L]
    RewriteRule ^/?([-a-zA-Z0-9_/]+)/?$ /index.php?area=$1 [L]
    It works OK except for the section home pages (About and Topics). They work OK online, and they generally function OK locally, but the URL is http://MySite/Topics/?area=Topics instead of http://MySite/Topics

    I also run into technical problems when working on my code; for example, it's harder assigning values to these pages, largely because of the weird URL's.

    Is there some trick I can use to make that third rewrite rule ignore all my static pages and stop interfering with them?

    Thanks.

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,312
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    The problem is that the third rule will capture most everything. So after About/whatever is rewritten to About/index.php?about=whatever, the third rule will then re-rewrite to index.php?area=About/index.php. What you'll probably need to do is attach some conditions to the third rule.

    # This condition reads:
    # The path matched by the rule must not begin with "About" or "Topics"
    RewriteCond $1 !^(?:About|Topics)(?:$|/)


    You also mentioned that it's harder to assign values to these pages -- I'm guessing you mean assigning values through the query string -- and that's because when your rewritten URL contains a query string, then it replaces the existing query string. But you can fix that problem with the QSA flag, which will add any existing query string to the end of your rewritten query string.

    RewriteRule ^About/([a-zA-Z0-9()_/-]+)/?$ About/index.php?bout=$1 [L,QSA]
    "First make it work. Then make it better."

  3. #3
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip; it works great.


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
  •