URL Rewriting in Wordpress for only one specific page

admin2025-01-07  4

I have a Wordpress website where I created a page called "products". In this page, I have a php script that get a parameter ("CD_Produit") from the URL, and show a specific result.

For example, I'm typing the url "www.mywebsite/products/?CD_Produit=BP200" and I'd like the url to be shown as "www.mywebsite/products/BP200/", and only for this specific url.

I still want the url "www.mywebsite/products/?CD_Produit=XYZ" to show "www.mywebsite/products/?CD_Produit=XYZ".

Of course, to make it easy, I'm using a "$_POST[]" to get the url parameter in my script, and that could be great that it still work even with the specific rewriting.

Is this even possible ?

I tried to play a bit with the .htaccess file, but not sure exactly what I am doing.

That doesn't work (where "p=3653" is the id of my page called "products"):RewriteRule ^/products/BP200/ /index.php?p=3653&CD_Produit=BP2000 [L]

This doesn't work either: RewriteRule ^/products/BP200/ /products?CD_Produit=BP2000 [L]

I use the "normal" htaccess generated by Wordpress:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This is about programming in the way it is speaking about php script and url-rewriting

I have a Wordpress website where I created a page called "products". In this page, I have a php script that get a parameter ("CD_Produit") from the URL, and show a specific result.

For example, I'm typing the url "www.mywebsite.com/products/?CD_Produit=BP200" and I'd like the url to be shown as "www.mywebsite.com/products/BP200/", and only for this specific url.

I still want the url "www.mywebsite.com/products/?CD_Produit=XYZ" to show "www.mywebsite.com/products/?CD_Produit=XYZ".

Of course, to make it easy, I'm using a "$_POST[]" to get the url parameter in my script, and that could be great that it still work even with the specific rewriting.

Is this even possible ?

I tried to play a bit with the .htaccess file, but not sure exactly what I am doing.

That doesn't work (where "p=3653" is the id of my page called "products"):RewriteRule ^/products/BP200/ /index.php?p=3653&CD_Produit=BP2000 [L]

This doesn't work either: RewriteRule ^/products/BP200/ /products?CD_Produit=BP2000 [L]

I use the "normal" htaccess generated by Wordpress:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This is about programming in the way it is speaking about php script and url-rewriting

Share Improve this question asked Dec 2, 2024 at 13:05 JulianJulian 132 bronze badges 2
  • Why do you only want it for that one specific URL? That sort fo granularity is both ungainly to implement and inconsistent for users – Chris Cox Commented Dec 2, 2024 at 13:18
  • Ok, let's say that I want all the pages "www.mywebsite.com/products/?CD_Produit=XXX" to be shown as "www.mywebsite.com/products/XXX/" then ! – Julian Commented Dec 2, 2024 at 22:32
Add a comment  | 

1 Answer 1

Reset to default 2

Let's start by getting rid of the rules you've added to your .htaccess. This is WPSE and the page is running inside of WordPress, so we're going to rely on WP to do the rewriting.

You're going to want to register CD_Produit as a query variable so WordPress' front controller is aware of it.

function wpse427745_add_query_vars($vars) {
  $vars[] = 'CD_Produit'; 
  return $vars;
}


add_filter('query_vars', 'wpse427745_add_query_vars');

Next you need to add the rewrite:

function wpse427745_add_rewrite_rule($rules) {
  $new_rules = array('CD_Produit/([^/]+)/?$' => 'index.php?pagename=products&CD_Produit=$matches[1]');
  $rules = $new_rules + $rules;
  return $rules;
}
 
add_filter('rewrite_rules_array', 'wpse427745_add_rewrite_rules');

Visit Settings > Permalinks and hit "Save Changes" (which refreshes all permalinks, not just the ones listed on the Settings page) and you should be good to go.

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

最新回复(0)