Support » Fixing WordPress » Exclude from wordpress rewrite rules

  • Attempting to exclude a folder ‘test’ from the wordpress rewrite rules in the htaccess file and give it a unique rewrite. Test isn’t a real folder.

    So this: /test/post1/ or this /test/post1 should rewrite to /index.php?pagename=$1 where $1 is the posttitle e.g. post1. I think I have the exclusion working as a RewriteCond (see below) – how do I add the RewriteRule for it?

    # BEGIN WordPress
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_URI} !^/(test|test/.*)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
Viewing 3 replies - 1 through 3 (of 3 total)
  • The following will probably work:

    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteRule ^/test/(\w+)/$ /index.php?pagename=$1 [L]
    RewriteRule ^/test/(\w+) /index.php?pagename=$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    The first new rule is probably not necessary; you should check whether it works with only the second rule.

    Note that for security reasons, the new rules limit the page names to only having letters, numbers, and underscores. You should make every effort to abide by this limitation, but if you cannot, then the rules must be changed accordingly. Also note that if you remove the limitation, you will definitely need two rules.

    Thanks but nope neither works. I had already tested with something similar:

    RewriteRule ^/test/([^/]*)-([0-9]+)/$ index.php?pagename=$1 [L]

    which doesn’t work either. Everything I’ve tried just does a redirect instead of a rewrite so for example /test/post1/ just changes in the browser address bar to /post1/.

    I was wondering if the rewrite to index.php was causing a particular issue. So I made a dummy test.php page and tried this:

    RewriteCond %{REQUEST_URI} ^/(test|test/.*)$
    RewriteRule . /test.php?pagename=$1 [L]

    That works but I couldn’t get this to work:

    RewriteRule ^/(test|test/.*)$ /test.php?pagename=$1 [L]

    Anyway I could duplicate the index.php page in test.php as a work around – I’d rather not, if anyone knows how to get it to work that would be greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Exclude from wordpress rewrite rules’ is closed to new replies.