Amit Bera
Amit Bera

Reputation: 7611

Wordpress htaccess redirection aliases are not working

I have a few links in the following format

Which I want redirecting to the following format

I've added the following code in to the htaccess file, but it's not working and gives a 404 message

RewriteRule ^ranges/([^/]+)/? ranges?param1=$1

The full htaccess file looks like this

# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /india
RewriteRule ^ranges/([^/]+)/? ranges?param1=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /india/index.php [L]
</IfModule>

# END WordPress

To clarify, I need redirection to work like this;

http://127.0.0.1/india/ranges/MyValue1/ > http://127.0.0.1/india/ranges/?param1=MyValue1

Any suggestions for how I can achieve this using htacces or any other way?

Upvotes: 1

Views: 101

Answers (1)

Spartacus
Spartacus

Reputation: 1508

You'll want to use add_rewrite_rule() instead of editing your htaccess. I hate to assume, but you didn't provide any detail; if ranges is a custom post type (EDIT: turns out 'ranges' was a page, answer edited), then add this to your function.php file:

function stack_45948362_rewrite() {
    add_rewrite_rule('^ranges/([^/]+)?$','index.php?pagename=ranges&query=$matches[1]','top' );
}
add_action( 'init', 'stack_45948362_rewrite', 10, 0);

Then add 'query' as a query var (although I'd choose a little less generic name to avoid collision).

function stack_45948362_qvars($vars) {
    $vars[] = 'query';
    return $vars;
}
add_filter( 'query_vars', 'stack_45948362_qvars', 10, 1);

Make sure to flush your permalinks by going to Settings > Permalinks and clicking 'save changes'.

Most likely your 404 is being caused by "ranges?query=", because Wordpress is looking for an actual php page to process.

Upvotes: 1

Related Questions