url rewriting - I need a custom permalink for my website

admin2025-06-04  1

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.

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

最新回复(0)