Asked  1 Year ago    Answers:  5   Viewed   12 times

I have links in my site that pass queries to pages that query from an external database. This works fine e.g.

mysite.com/catalog/?tl=flooring

however i want to rewrite this url to appear as

mysite.com/catalog/flooring

Ive tried modifying the rewrite rules in wordpress but it always displays the index page

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['(catalog)/([a-zA-Z0-9 ]+)$'] = '/catalog/?tl=$matches[2]';
    return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
    array_push($vars, 'id');
    return $vars;
}

 Answers

1

Rewrite rules in WordPress don't quite work like how you're expecting them to. All rewrite rules map to a file handler (almost always index.php), not another URL.

Here is a shortened code example;

$rules['catalog/(.*)/?'] = 'index.php?pagename=catalog&tl=$matches[1]';
array_push($query_vars, 'tl'); // note query var should match your query string

This would map catalog/whatever to the WordPress page 'catalog', and pass along the query var 'tl'. You could create a page template for 'catalog', then pick up the value of 'tl' using get_query_var('tl').

Also avoid using query vars like id - use something more unique (like 'tl') to avoid clashes with WordPress.

And don't flush rules on every init! It's bad practice, and will write to .htaccess and call database updates on every page load!

WordPress will always flush permalinks whenever you update a post or your permalink structure (simply update your permalinks when you make changes to your code).

Saturday, May 29, 2021
 
2

To display the value of custom fields in your loop you can use this snippet of code:

<?php query_posts( 'post_type=music'); ?>
  <?php while ( have_posts() ) : the_post(); ?>
   <?php get_template_part( 'content',  get_post_format() ); ?>

   <?php $what_name_you_want=get_post_meta($post->ID,'Your Custom Field Name',true); ?>

    <?php echo $what_name_you_want; ?>// This call the value of custom field


                <?php comments_template( '', true ); ?>
            <?php endwhile; // end of the loop. ?>

Tell me if it works!

Thursday, April 1, 2021
 
Ula
 
Ula
3

If your server is Apache: Create on root folder file ".htaccess"

#.htaccess    
RewriteEngine On
Options +FollowSymlinks
RewriteRule /register index.php?mode=register

//index.php

<?php
if(isset($_GET['mode']=='register')){
      include('includes/register.php');
} 
?>
Thursday, April 1, 2021
 
Sauleil
 
3

You simply have to set /blog/%postname%/ as your permalinks structure, this will not change your pages permalinks.

And to keep your portfolio permalinks, you should set with_front to false when you register this post type.

'with_front' => bool Should the permastruct be prepended with the front base. (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/). Defaults to true

EDIT 1 : You should probably flush Wordpress rewrite rules after that.

EDIT 2 : with_front param is a rewrite param :

'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
Wednesday, July 21, 2021
 
akohout
 
2

You seem to have misunderstood what the rewrite module (and your rules specifically) actually does.

Quite simply when you browse to:

 localhost/abc/product.php?id=23

The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the wrong URL

URL Transformation

http://www.yoursite.com/product/123         <- URL that user goes to
http://www.yoursite.com/product.php?id=123  <- Rewritten URL that the server sees

RewriteRule(s) explanined

A rewrite rule is broken down into three parts (not including the RewriteRule part...)

  1. The regex that it matches against the url
  2. The url that it transforms into
  3. Additional options

Given your rule:

RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]

The regex is: ^product/([^/.]+)/?$
The new url : product.php?id=$1
The options : [L]

This means that the user browses to the nice url http://www.yoursite.com/product/123 (and all of your links point to the nice URLs) and then the server matches against the regex and transforms it to the new URL that only the server sees.

Effectively, this means that you have two URLs pointing to the same page... The nice URL and the not-nice URL both of which will take you to the same place. The difference is that the not-nice / standard URL is hidden from the general public and others pursuing your site.


Regex

The reason why http://mysite.com/product/image.jpg is not being redirected is because of the regex that you use in your RewriteRule.

Explanation

^product/([^/.]+)/?$
  • ^ => Start of string
  • product/ => Matches the literal string product/
  • ([^/.]+) => A capture group matching one or more characters up until the next / or .
  • /?$ => Matches an optional / followed by the end of the string

Given the URL:

http://mysite.com/product/image.jpg

Your regex matches product/image but then it encounters . which stops the matching... As that isn't the end of string $ the rule is invalidated and thus the transform never happens.

You can fix this by changing your character class [^/.] to just [^/] or - my preferred method - to remove the character class and simply use .+ (seeing as your capturing everything to the end of the string anyway

RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/(.+)/?$ product.php?id=$1 [L]
Thursday, August 5, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :