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 wordssite/dictionary/?w=word
shows the definition of the wordnow 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 wordssite/dictionary/?w=word
shows the definition of the wordnow 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.
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/