How can keep my existing rewrite rules working with a WordPress install?

I had a pretty simple website that had some static pages, but also some dynamic pages that use rewrite rules to pull data in. For instance:

RewriteRule ^phone-directory/([^/.]+).htm$ number.php?for=$1 [L]

So any urls like: phone-directory/google.htm would always load number.php with google on the query string.

Read More

I have installed WordPress for the CMS capabilities but I still need my directory working at the same time. I also want the directory to use the WordPress theme so that styling changes are site wide!

So I set up a custom template in my WP theme directory, and set up a “page” in word press called “company-listing”. I then added the following rule to the top of my htaccess file:

RewriteRule ^phone-directory/([^/.]+).htm$ index.php?p=23 [L]

p=23 refers to the location of my custom page “company-listing”

That rewrite rule works and takes me to “company-listing”, but that’s the problem, the URL rewrites to domain.com/company-listing

I don’t want it to do that. I think my rewrite rule is triggered, then when index.php?p=23 gets hit the default WordPress rewrite rule triggers. I have tried the following exludes to stop this from happening with no joy:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(phone-directory/.*)$
RewriteCond %{REQUEST_URI} !^/(index.php?p=23)$
RewriteCond %{REQUEST_URI} !^/(company-listing/.*)$
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Is there any way round this? To use a custom template in WordPress and rewrite to a url of my choice? In summary:

I want:

domain.com/phone-directory/google.htm

to load:

domain.com/index.php?p=23 (this is a “page” in wordpress)

Related posts

Leave a Reply

1 comment

  1. You just need to have your rules before your wordpress rules:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^phone-directory/([^/.]+).htm$ number.php?for=$1 [L]
    RewriteRule ^phone-directory/google.htm$ index.php?p=23 [L]
    
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    

    If one of the earlier rules matches, as long as number.php exists, the !-f condition for wordpress’ routing rule will fail.