Custom rewrite rule

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.

http://example.com/directorio/?filter=all&search_text=&type=medicos&location=quito&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

And I wanna make it look like:

http://example.com/directorio/medicos/quito/

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%', '([^/]+)' );

    add_rewrite_rule(
        'directorio/([a-z]+)/([a-z]+)/?$',
        'index.php?pagename=directorio&filter=all&search_text=&type=$matches[1]&location=$matches[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',
        '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?

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]
</IfModule>
# END WordPress

Leave a Comment