最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rewrite rules - URL rewriting in wordpress using parameters

programmeradmin1浏览0评论

This seems like a fairly simple idea, and forgive me for my ignorance, but in searching and testing various options, none of them work at all. I have tried a number of htaccess ideas and also WordPress's add_rewrite_rule the following:

  1. /
  2. /custom-url-rewrites-in-wordpress/
  3. Custom rewrite not working

I have a WordPress url that looks like this:

/?listing_category=Stay

I want the URL to look like this:

/

If I can get that working, it should then be straight forward to add additional parameters to the URL and build user friendly permalinks such as:

/?country=Italy&listing_category=Stay => /

Any guidance on this would be most appreciated ;-)

Here is some of the code I have tried:

Option 1

function rewriteurl() {
  global
  $wp,$wp_rewrite;
  $wp->add_query_var('listing_category');
  $wp_rewrite->add_rule('^/([^/]*)/?$', 'index.php?listing_category=$matches[1]', 'top');

  // Reset permalinks, delete after programming
  $wp_rewrite->flush_rules(true);  
}
add_action( 'init', 'rewriteurl' );

Option 2

function custom_rewrite_rule() {
    add_rewrite_rule('^([^/]+)?','index.php?listing_category=$matches[1]','top');
    add_rewrite_tag('%listing_category%', '(.*)');
    flush_rewrite_rules();
} 
add_action('init', 'custom_rewrite_rule');

This seems like a fairly simple idea, and forgive me for my ignorance, but in searching and testing various options, none of them work at all. I have tried a number of htaccess ideas and also WordPress's add_rewrite_rule the following:

  1. https://wordpress.stackexchange.com/questions/56417/url-rewrite-with-add-rewrite-rule-and-attachment-id/
  2. https://matty.blog/custom-url-rewrites-in-wordpress/
  3. Custom rewrite not working

I have a WordPress url that looks like this:

https://mywebsite.com/?listing_category=Stay

I want the URL to look like this:

https://mywebsite.com/stay/

If I can get that working, it should then be straight forward to add additional parameters to the URL and build user friendly permalinks such as:

https://mywebsite.com/?country=Italy&listing_category=Stay => https://mywebsite.com/italy/stay/

Any guidance on this would be most appreciated ;-)

Here is some of the code I have tried:

Option 1

function rewriteurl() {
  global
  $wp,$wp_rewrite;
  $wp->add_query_var('listing_category');
  $wp_rewrite->add_rule('^/([^/]*)/?$', 'index.php?listing_category=$matches[1]', 'top');

  // Reset permalinks, delete after programming
  $wp_rewrite->flush_rules(true);  
}
add_action( 'init', 'rewriteurl' );

Option 2

function custom_rewrite_rule() {
    add_rewrite_rule('^([^/]+)?','index.php?listing_category=$matches[1]','top');
    add_rewrite_tag('%listing_category%', '(.*)');
    flush_rewrite_rules();
} 
add_action('init', 'custom_rewrite_rule');
Share Improve this question edited Feb 22, 2022 at 19:31 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 2, 2022 at 16:32 Laurence TuckLaurence Tuck 9402 gold badges10 silver badges21 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

I ended up scrapping the WordPress solutions described and WordPress all together for this project and building my own basic front end.

Using this answer: https://technicalseo.com/tools/htaccess/ I was able to create a rule for each possibility.

Bear in mind, the URL must contain index.php in order to work and URL parameters are not passed to the script unless QSA is appened, but this adds them to the URL which defeats the purpose.

Here is what I ended up doing in my htaccess file:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^loc=(.*)&cat=(.*)&sub=(.*)$
RewriteRule ^index.php$ /%1/%2/%3/? [L,R=301]

RewriteCond %{QUERY_STRING} ^loc=(.*)&cat=(.*)$
RewriteRule ^index.php$ /%1/%2/? [L,R=301]

RewriteCond %{QUERY_STRING} ^loc=(.*)$
RewriteRule ^index.php$ /%1/? [L,R=301]

RewriteCond %{QUERY_STRING} ^name=(.*)$
RewriteRule ^index.php$ /%1/? [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

I'm sure there is a way to do this in WordPress and I got close using the Redirection plugin, but ran out of time after testing a few hundred possibilities.

Be careful with Redirects and caching, also if using WordPress you need to update your permalinks each time you change a rule.

发布评论

评论列表(0)

  1. 暂无评论