$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - I need a custom permalink for my website|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - I need a custom permalink for my website

matteradmin8PV0评论

I have following scenario:

my-domain-name/google

my-domain-name/twitter

I need a custom permalink structure to pass google or twitter to domain_name query variable.

I have tried following code:

add_rewrite_tag('%domain_name%', '([^&]+)');
add_rewrite_rule('^/([^/]*)/?','index.php?domain_name=$matches[1]','top'); 

But not working

For better example Check this site: I am trying to do the similar

I have following scenario:

my-domain-name/google

my-domain-name/twitter

I need a custom permalink structure to pass google or twitter to domain_name query variable.

I have tried following code:

add_rewrite_tag('%domain_name%', '([^&]+)');
add_rewrite_rule('^/([^/]*)/?','index.php?domain_name=$matches[1]','top'); 

But not working

For better example Check this site: http://currentlydown/facebook I am trying to do the similar

Share Improve this question edited Jan 5, 2019 at 8:58 Saurin Dashadia asked Jan 4, 2019 at 13:46 Saurin DashadiaSaurin Dashadia 1757 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

I'm not sure if I should assume that you were issuing your examples inside of the init hook, but if not that is the reason they weren't working for you.

So inside functions.php

function sd_rewrite_rules() {
  add_rewrite_tag('%domain_name%', '([^&]+)');
  add_rewrite_rule('^/([^/]*)/?','index.php?domain_name=$matches[1]','top');
}
add_action('init', 'sd_rewrite_rules', 10, 0);

Then if you wanted to retrieve the query variable to do something with it, you would do something like this in a page template file using the $wp_query global.

$domain_value = $wp_query->query_vars['domain_name'];

Everything you would want to know plus more can also be located by reading all about the Rewrite_API and it has tons of examples of doing other things you might find interesting or relevent.

https://codex.wordpress/Rewrite_API

The problem was solved by just one line of code. I am thankful to @rifi2k for long discussion and proposing different solutions.

Here is the solutions:

Add following code to functions.php

function custom_rewrite_basic() {
    add_rewrite_tag('%domain_name%', '([a-z]{1,60}\.[a-z]{2,})');
}
add_action('init', 'custom_rewrite_basic');

Then from permalink settings, select Custom Structure and add newly created tag by only in our case its /%domain_name%/ save settings.

Post a comment

comment list (0)

  1. No comments so far