WordPress / mod_rewrite expertise needed

Discussion in 'WordPress' started by meeble, Nov 30, 2011.

  1. #1
    Hello,

    I am trying to use mod_rewrite and modify WordPress' URL handling to allow for passing variables to PHP from the URL while maintaining "pretty permalink" functionality. (on WP 3.2.1)

    Something like this:

    http:/epiclasers.com/hello-world
    (normal with permalinks set to /%postname%)

    http:/epiclasers.com/state/hello-world
    (WP should display the same hello-world post as above, but also would pass the state string to PHP as a variable)

    http:/epiclasers.com/state/city/hello-world
    (WP should display the hello-world post, but would pass the state and city strings to PHP as variables)

    I added the following mod_rewrite code to my .htaccess file:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(\w+)/(\w+)/ /index.php?st=$1&city=$2 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(\w+)/ /index.php?st=$1 [L]

    which kind of works. The variables are being passed, and PHP can grab them with:
    $state = $_GET['st'];<br>
    $city = $_GET['city'];

    The problem is that WP is getting confused by the URL and isn't finding the post. URL's like these:

    http:/epiclasers.com/state/city/hello-world
    http:/epiclasers.com/state/

    are bringing up a page not found error. Also, when entering this URL:

    http:/epiclasers.com/state/hello-world

    It redirects to: http:/epiclasers.com/hello-world
    which then displays the post fine.

    How do I modify the WP URL handling code to ignore the /state and /city parts of the URL without also breaking URLs like:

    http:/epiclasers.com/category/category1
    http:/epiclasers.com/tag/tag1
    http:/epiclasers.com/category/category2/subcategory

    etc.

    Thanks,
    Devin
     
    meeble, Nov 30, 2011 IP