url rewriting - Cleanup URL for a custom page in wordpress

admin2025-06-04  46

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/dictionary lists all the words
  • site/dictionary/?w=word shows the definition of the word

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

site/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: / but this also didn't work for me.

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/dictionary lists all the words
  • site/dictionary/?w=word shows the definition of the word

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

site/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/questions/27162489/ but this also didn't work for me.

Share Improve this question edited Jan 8, 2019 at 11:38 Mostafa Ahangarha asked Jan 8, 2019 at 9:19 Mostafa AhangarhaMostafa Ahangarha 1235 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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/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' );

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/the-simplest-php-router/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749029886a315765.html

最新回复(0)