htaccess help needed - static page on wordpress site

Discussion in 'Apache' started by jelv, Apr 25, 2008.

  1. #1
    My website is powered by wordpress (installed in the root level of my web directory). I have a static page/script, not controlled by wordpress, uploaded in the root level of my site (product.php). This page is used to dynamically display the product info for whatever product is chosen on product list pages. In my htaccess file I have the standard WordPress stuff that is necessary to make the site work:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    I have also added the following to the htaccess file:

    RewriteEngine On
    RewriteRule ^product/([0-9]+)/$ /product.php?sku=$1

    I’m trying to be able to use urls like "product/1234" but still grab the sku number as a GET variable on my static (not controlled by wordpress) product.php page.

    If I delete the wordpress stuff, my rewriterule works perfectly for the product page, but of course the rest of the site doesn’t work because the wordpress directives are gone. But with the wordpress stuff in place my rewriterule doesn’t work.

    I'm very new to htaccess (in fact this is my first time messing with it) and I'd be very grateful for any help you could give me. Why does my rule work if the wordpress stuff is gone but not when the wordpress stuff is present? How do I accomplish what my rule does without breaking the wordpress part of the site?
     
    jelv, Apr 25, 2008 IP
  2. Cybernaut

    Cybernaut Peon

    Messages:
    408
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do it before wordpress conds & rules. Like this:


    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # Your code first
    RewriteRule ^product/([0-9]+)/$ product.php?sku=$1 [color=red][L][/color]
    
    # WordPress start
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # WordPress end
    </IfModule>
    Code (markup):
    Note the L flag in your rule to stop the rewriting there if url matches /product/0123/.
     
    Cybernaut, Apr 25, 2008 IP
  3. jelv

    jelv Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That worked perfectly! Thanks a lot cybernaut!
     
    jelv, Apr 25, 2008 IP
  4. jelv

    jelv Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Cybernaut (or anyone else),

    Things are working just how I want them to on my site now. But for some reason, with this change we have made, my wordpress admin tool isn't letting me save a page. When I go to save a page, it gives me an internal server error. In fact, after trying to save a page in the admin tool my site no longer pulls up either (giving an internal server error instead) until I re-save my htaccess file. Any ideas?
     
    jelv, Apr 25, 2008 IP
  5. Cybernaut

    Cybernaut Peon

    Messages:
    408
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think wordpress is changing .htaccess. If your .htaccess file is writable by web server, make it read only (chmod) to see if that works.
     
    Cybernaut, Apr 25, 2008 IP
  6. jelv

    jelv Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Changing to read only worked. Kind of a pain to have to change it back to writable any time I need to make a change, but that really shouldn't happen very often. Thanks a lot for all the help!
     
    jelv, Apr 25, 2008 IP