How to make wordpress category's URL with slash at the end and without it in post's url. Like this:
"mysite.com/mycategory/" "mysite.com/mycategory/mypost"
The problem is that by default, you can either do everything with a slash or all without a slash. ('category' prefix already removed via htaccess).
There are two good solutions for this:
The WP_Rewrite class has a var named $use_trailing_slashes
that is set dynamically based upon whether or not your custom permalink structure ends in a '/'.
$this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );
This means that all WP generated links (the_permalink, category_link, the_permalink_rss, etc.) will not end in a '/'. So for category pages WP will show '/category/category' instead of '/category/category/'.
You can solve it by either using a filter or modifying your .htaccess or both:
Sample user_trailingslashit Filter
The user_trailingslashit function applies the 'user_trailingslashit' filter to the result prior to returning it. It provides the url and the type of url to the filter.
$string = apply_filters('user_trailingslashit', $string, $type_of_url);
So to hook into this and add a trailing slash to all urls other than single posts add this code to a plugin file or your functions.php theme file.
function fix_trailingsss($s='',$t='single')
{
if($t!='single')$s=rtrim($s,'/').'/';
return preg_replace('/^(.*)([^l/])$/i', '\1\2/',$s);
}
add_filter('user_trailingslashit', 'fix_trailingsss', 9999,2);
Htaccess RedirectMatch
You can setup an .htaccess redirect to force category urls to always use a trailing slash like this:
RedirectMatch 301 ^/category/([^/]+)$ /category/$1/
Source of information : https://www.askapache.com/wordpress/adding-trailing-permalinks/
For any further questions consult the codex:
I solved it like this:
function no_page_slash( $string, $type ){
if($type == 'single')
$string = untrailingslashit($string);
return $string;
}
add_filter('user_trailingslashit', 'no_page_slash', 70, 2);
Your permalinks must be set on like /%category%/%postname%/ .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.