2

I have a wordpress website in which I have a specific page for dictionary. This page is actually a custom page and does these things:

  • site.com/dictionary lists all the words
  • site.com/dictionary/?w=word shows the definition of the word

now I want the URL to be cleaned up and becomes like this:

site.com/dictionary/word

I have made these lines in .htaccess but I get 404 error:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /dictionary/
    RewriteRule (.*) ?w=$1 [L]
</IfModule>

May you help me with this?

update:

I have tried the solution here: https://stackoverflow.com/questions/27162489/ but this also didn't work for me.

2 Answers 2

3

You can do this with the internal rewrite system, which is parsed in php, not htaccess.

First, add the rule. This assumes you have created a root page under Pages with the slug dictionary.

function wpd_dictionary_rewrite(){
    add_rewrite_tag( '%dictionary_word%', '([^/]+)' );
    add_rewrite_rule(
        '^dictionary/([^/]+)/?$',
        'index.php?pagename=dictionary&dictionary_word=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpd_dictionary_rewrite' );

This code would go in your theme's functions.php file, or your own plugin.

Visit the Settings > Permalinks page to flush rules after adding this.

Now you can visit site.com/dictionary/word and the requested word will be available in the template with get_query_var('dictionary_word').

If the code relies on $_GET['w'] and you can't / don't want to change this, you can hook before the code runs and set the value manually:

function wpd_set_dictionary_word(){
    if( false !== get_query_var( 'dictionary_word', false ) ){
        $_GET['w'] = get_query_var( 'dictionary_word' );
    }
}
add_action( 'wp', 'wpd_set_dictionary_word' );
4
  • Tnx. But what do you mean I should flush rules in settings > Permalinks? I have tried this answer already (without flushing Permalinks) and it didn't work. Any further input? Also, can I put these codes in the relevant page itself not in function.php? Everything is managed in a single template file. Jan 9, 2019 at 17:49
  • 1
    Visiting the Permalinks Settings page causes WordPress to flush the cache of rewrite rules. If you add/change rules and don't flush the cache, they won't work. You have to put this code in a file that gets loaded before init, the simplest is functions.php. Template files won't work, they don't get loaded until much later.
    – Milo
    Jan 9, 2019 at 18:47
  • I put the rewrite function and action into function.php file. I flush Permalinks as well. I prefer to use get_query_var. I don't get 404 error anymore (which I guess means rewrite is in action). But still I cannot get the value or the variable by get_query_var. Jan 26, 2019 at 18:02
  • I found the problem: I was using w as the key for query string and by changing it into term the problem was solved. Jan 27, 2019 at 9:44
1

You can't just rewrite the URL.

You would need to refactor a lot code when using that other URL path /word over an URL parameter ?w=word.

That are two different pairs of shoes...

some more info:

Routing with PHP in WordPress is a complex topic and not easy to describe in a few words. Even as an experienced developer, you probably won't want to be confronted with routing problems.

Here there is a short example on how PHP routing can work: https://www.taniarascia.com/the-simplest-php-router/

3
  • Why is it so? any reference? why isn't it possible to use htaccess in wordpress? Jan 8, 2019 at 9:47
  • 1
    you can use htaccess. but your problem is a different. i did add some more info to my post Jan 8, 2019 at 10:02
  • what if I rewrite to index.php?page_id=1234&w=word? I have written RewriteRule ^diction/(.+)$ diction/?w=$1 [L,R] and it works with redirecting to the page. but if I remove flag R, I get 404. why is it happening? I don't want to go for php routing if I can do it with htaccess Jan 8, 2019 at 10:09

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.