I need to do some URL Rewriting, but that is all new to me and I've tried a few things but nothing seems to work. For example: I have an URL that looks like this.


And I wanna make it look like:


I've tried something like this directly into my .htaccess.

RewriteRule ^directorio/([a-z]+)/([a-z]+)/?$ index.php?pagename=directorio&filter=all&search_text=&type=$1&location=$2&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search [L]

That doesn't work. Also tried...

add_action('init', 'my_custom_rewrites' );
function my_custom_rewrites(){

    add_rewrite_tag( '%filter%', '([^/]+)' );
    add_rewrite_tag( '%search_text%', '([^/]+)' );
    add_rewrite_tag( '%type%', '([^/]+)' );
    add_rewrite_tag( '%location%', '([^/]+)' );
    add_rewrite_tag( '%cs_directory_search_location%', '([^/]+)' );
    add_rewrite_tag( '%search_view%', '([^/]+)' );
    add_rewrite_tag( '%goe_location_enable%', '([^/]+)' );
    add_rewrite_tag( '%cs_loc_max_input%', '([^/]+)' );
    add_rewrite_tag( '%cs_loc_incr_step%', '([^/]+)' );
    add_rewrite_tag( '%submit%', '([^/]+)' );
    add_rewrite_tag( '%action%', '([^/]+)' );

        'top' );

I also read a bit about query vars, but as I said before, it is a theme we bought and it accesses all the variables in the query string using $_GET and not get_query_var...

I'm not sure what's the problem there, but I'm out of ideas, it's a tough way to start learning RewriteRules. Hoping someone can help.

Hi @Milo, thanks for your answer, one more thing, I have my site live and also the same site on my localhost to test changes before uploading them. The thing is, in my localhost, I edited my .htaccess directly and used this rule.

RewriteRule ^directorio/([a-z]+)/([a-z]+)/?$ index.php/directorio/?filter=all&search_text=&type=$1&location=$2&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search [L]

And that rule, with that ugly url, works perfect, but If I put that same rule on the live site it doesn't work at all. Also, if on that live site, if I use that same rule, but with the absolute path, I get the redirect with no problems, it just wont let me do the Rewrite. Is it possible that my hosting has some configuration on the Apache server causing that problem?

  • If the code accesses $_GET, you will need to populate $_GET manually from the WordPress query vars set in your rewrite rules. Despite looking like a query string, WordPress doesn't set any request globals from that string in add_rewrite_rule.
    – Milo
    Commented Aug 29, 2016 at 21:44

1 Answer 1


Add the following to your .htaccess, in between the <IfModule mod_rewrite.c>:

RewriteCond %{QUERY_STRING} (^|&)filter=all&search_text=&type=(.*)&location=(.*)&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search
RewriteRule ^(.*)$ /directorio/%2/%3/? [R=301,L]

You can confirm the logic through this .htaccess tester. Your default .htaccess created from WordPress should now look like the following:

# 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]
# Custom RewriteRule
RewriteCond %{QUERY_STRING} (^|&)filter=all&search_text=&type=(.*)&location=(.*)&cs_directory_search_location=Yes&search_view=&goe_location_enable=No&cs_loc_max_input=5&cs_loc_incr_step=1&submit=&action=cs_directory_map_search
RewriteRule ^(.*)$ /directorio/%2/%3/? [R=301,L]
# END WordPress
  • Hi, thanks for your answer, I have one question, does that Redirection works with regular expressions? It says, it does but I can't get it working. If you could help me with an example on how to do it, it would be great. Also, putting the redirection directly into my .htaccess doesn't work either.
    – Dysan G.
    Commented Sep 5, 2016 at 16:43
  • @DysanG. Yes it does. Make sure that you check off the "Regular expression" checkbox when adding your rules. I've updated my answer. Check if that works. Commented Sep 5, 2016 at 19:30
  • no luck, the plugin works great with plain redirection, but as soon as I activate Regular Expression options it doesn't, the regex query is fine, but the plugin is still trying to make a plain redirection.
    – Dysan G.
    Commented Sep 8, 2016 at 21:34
  • @DysanG. Figured it out, check out my updated and and please confirm. Commented Sep 13, 2016 at 2:29
  • Hi Ethan, thanks for your help, but that's not working either, I'm gonna have to take the long road and rewrite the code to generate the urls I want directly.
    – Dysan G.
    Commented Sep 13, 2016 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.