admin管理员组

文章数量:1122846

Is it possible to change the URL structure for the specific URL only?

Currently: /?brochure_id=new-year-guide

Desired:

I tried with:

RewriteRule brochure/([^&]+)/ brochure/?brochure_id=$1 [L]
RewriteRule brochure/([^&]+) brochure/?brochure_id=$1 [L]

SOLVED:

I used WordPress built-in add_rewrite_rule function:

In functions.php

add_action('init', 'dcc_rewrite_tags');
function dcc_rewrite_tags() {
    add_rewrite_tag('% brochure_id%', '([^&]+)');
}

function custom_rewrite_rule() {
    add_rewrite_rule('^ brochure/([^/]*)/([^/]*)/?','index.php?page_id=99&brochure_id=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

page_id=99 is ID of page that is connected to my template.

Within the template:

global $wp_query;
$brochure_slug = $wp_query->query_vars['brochure_id'];

Is it possible to change the URL structure for the specific URL only?

Currently: https://www.website.com/brochure/?brochure_id=new-year-guide

Desired: https://www.website.com/brochure/new-year-guide

I tried with:

RewriteRule brochure/([^&]+)/ brochure/?brochure_id=$1 [L]
RewriteRule brochure/([^&]+) brochure/?brochure_id=$1 [L]

SOLVED:

I used WordPress built-in add_rewrite_rule function:

In functions.php

add_action('init', 'dcc_rewrite_tags');
function dcc_rewrite_tags() {
    add_rewrite_tag('% brochure_id%', '([^&]+)');
}

function custom_rewrite_rule() {
    add_rewrite_rule('^ brochure/([^/]*)/([^/]*)/?','index.php?page_id=99&brochure_id=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

page_id=99 is ID of page that is connected to my template.

Within the template:

global $wp_query;
$brochure_slug = $wp_query->query_vars['brochure_id'];
Share Improve this question edited Jun 21, 2018 at 14:03 SlavisaPetkovic asked Jun 21, 2018 at 10:58 SlavisaPetkovicSlavisaPetkovic 11 silver badge2 bronze badges 1
  • For anybody else who reads this, the solution edited in is the correct answer and the most portable answer. If this was done via HTAccess then WP may then redirect you back because it's no longer the canonical URL, WP rewrite rules and redirects are what should be used here, with the added plus that they will work regardless of wether Nginx/Apache/etc are used – Tom J Nowell Commented Feb 10, 2022 at 18:43
Add a comment  | 

1 Answer 1

Reset to default 0

EXAMPLE 1 - E-COMMERCE SITE

Sad Original URL = site.com/page.php?category=2&product=54

Happy URL :) = site.com/sandwiches/rueben-sandwich/

step 1:

Make sure that all category names and product names are unique in your database.

step 2:

Replace all references to Original URL with the New URL throughout your website.

step 3:

Use mod_rewrite in your .htaccess file to parse out the elements of the URL. Like this:

RewriteEngine On

RewriteRule /(.*)/(.*)/$ page.php?category=$1&product=$2

the (.*) pulls the elements out and puts them in variables $1 and $2.

step 4:

Update your code on the page.php file to get the data from the database via the category and product name instead of by ID (this is why the names must be unique). For example:

Before:

"select * from database_table where categegory_id='$category' and product_id='$product'"

After:

"select * from database_table where categegory_name='$category' and product_name='$product'"

NOTE: This is just a quick and simple example that doesn't consider sanitizing input for security (which you must do) or any table joins you might need to do on your site.

本文标签: Change dynamically URL to SEO friendly via htaccess