WordPress .htaccess rewrite rule to add a variable to a given page

I have a wordpress site that is already using the .htaccess file to display clean URLS such as example.com/post-name

The wordpress .htaccess file contains the following code normally to do this:

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

# END WordPress

My problem is that I have special page that has a conditional statement in it based on a variable I want to pass through the URL. This URL actually works if I type it in the browser and functions properly: http://example.com/dir/?var=var-value

What I want to do is make this url clean by adding a new redirect rule to make the url look like this: http://example.com/dir/var-value. What I have added to the file is below:

RewriteRule ^dir/([^/]*)$ /dir/?var=$1 [L]

I don’t have a good understanding of regex especially in a .htaccess file and I have not been able to figure this out. If I add this redirect and then type in the appropriate url I just get a page not found error. I have already tried moving my rule to different locations within the file and I get the same result.

Additional information is that “dir” is an actual page created by the wordpress admin, I assume there is some conflict here. I did find this page on wordpress that could be helpful http://codex.wordpress.org/Rewrite_API/add_rewrite_rule however I can’t get it to work, I don’t know if I put this in my functions.php or in my custom template file.

I am using a child theme of the genesis framework that I created.

Related posts

Leave a Reply

1 comment