Rewriting query string with htaccess without conflicting with WordPress

I’ve setup a Rewrite rule =

RewriteRule ^names/([a-zA-Z0-9_]+|)(-in-|)([a-zA-Z0-9_]+|)(-|)([a-zA-Z0-9_]+|)(-|)([0-9]+|)/?$ query.php?sector=$1&location=$3&firmname=$5&firmnumber=$7

which works as expected i.e. when typing in /names/something_else-in-somewhere-somename-100
it passes these variable values to the query.php page.

Read More

This is being built on a site using WordPress and I’m having problems with what would appear to be the default WordPress htaccess rules preventing me from using the rewrite rule above without the /names/ – as I’d like to just have the query string as the url.

However if I remove the ^names/ from the rewrite rule, it stops the WordPress pages from being accessible. I’ve placed my rewrite code at the top of the htaccess document see below:

RewriteEngine On
RewriteBase /
RewriteRule ^names/([a-zA-Z0-9_]+|)(-in-|)([a-zA-Z0-9_]+|)(-|)([a-zA-Z0-9_]+|)(-|)([0-9]+|)/?$ query.php?sector=$1&location=$3&firmname=$5&firmnumber=$7 
ErrorDocument 404 /404.php 
RewriteRule ^index.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress

If I place the rewrite rule below the WordPress rules, it stops working all together. Can anyone shed any light on why this might be and point me in the right direction towards fixing this?

Related posts

Leave a Reply

2 comments

  1. Try this. Add another RewriteBase after the last line of WP htaccess

    RewriteBase /names/
    RewriteRule ^names/([a-zA-Z0-9_]+|)(-in-|)([a-zA-Z0-9_]+|)(-|)([a-zA-Z0-9_]+|)(-|)([0-9]+|)/?$ query.php?sector=$1&location=$3&firmname=$5&firmnumber=$7
    
  2. You must use [L] to mark end of your rule. Change your rule to:

    RewriteRule ^names/([a-zA-Z0-9_]+|)(-in-|)([a-zA-Z0-9_]+|)(-|)([a-zA-Z0-9_]+|)(-|)([0-9]+|)/?$ query.php?sector=$1&location=$3&firmname=$5&firmnumber=$7 [L,QSA,NC]