Search code examples
phpwordpressmod-rewritepermalinks

Wordpress Permalinks: Remove tag base and add the .html extension


My question:

My permalink settings:

General settings> Custom > /%postname%.html

Optional settings> Tag base > tag


I want to like this: mysite.com/%tag-slug%.html

Thanks


Edited for @Dimitry

was exactly the way I want

<?php
/*
Plugin Name: Custom tag URLs
Description: Appends .html to tag links
*/

// applied when calling get_tag_link()
add_filter('tag_link', 'my_tag_link', 10, 2);

/**
 * Returns a link to a tag. Instead of /tag/tag-name/ returns /tag-name.html
 */
function my_tag_link($tag_link, $tag_id) {
    $tag_base = get_option('tag_base');
    if ($tag_base) {
        // problem. returning: http://www.domain.com/post-tag/tag-name
        //$tag_link = preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link);

        // I added it. Result: http://www.domain.com/tag-name
        $tag_link = str_replace("$tag_base/", "", preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link)); 
        //echo "$tag_link<br>";
    }
    // problem. returning: http://www.domain.com/http://www.domain.com/tag-name.html
    //return '/' . trim($tag_link, '/') . '.html';

    // I added it. Result: http://www.domain.com/tag-name.html , 
    return trim($tag_link, '/') . '.html';

}
?>

I did the permalink settings

General settings> Custom > /post-%postname%.html Optional settings> Tag base > post-tag

.htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# Rewrites /tag-name.html as /post-tag/tag-name
# Assuming that your tag_base option is blank or set to 'post-tag'
RewriteCond %{REQUEST_URI} !^/post-.*
RewriteRule ^/?([^/]*)\.html$ /post-tag/$1

Single pages: http://www.domain.com/post-hello-world.html >> is working Tag Pages : http://www.domain.com/tag-name.html >> Not found 404 http://www.domain.com/post-tag/tag-name >> working

Problem:

Tag pages can not be found

I'm sorry. I am beginner. Thank you Dimitry


Solution

  • I'm not sure there is anything you can do through the admin interface, but you can add a little of your own code as a plugin (inside wp-content/plugins/tag_links.php):

    <?php
    /*
    Plugin Name: Custom tag URLs
    Description: Appends .html to tag links
    */
    
    // applied when calling get_tag_link()
    add_filter('tag_link', 'my_tag_link', 10, 2);
    
    /**
     * Returns a link to a tag. Instead of /tag/tag-name/ returns /tag-name.html
     */
    function my_tag_link($tag_link, $tag_id) {
        $tag_base = get_option('tag_base');
        if ($tag_base) {
            $tag_link = preg_replace('@^' . preg_quote($tag_base, '@') . '@', '', $tag_link); 
        }
        return '/' . trim($tag_link, '/') . '.html';
    }
    

    And in your .htaccess file, above other rewrite rules, do something like:

    # Rewrites /tag-name.html as /post-tag/tag-name
    # Assuming that your tag_base option is blank or set to 'post-tag'
    RewriteCond %{REQUEST_URI} !^/post-.*
    RewriteRule ^/?([^/]*)\.html$ /post-tag/$1
    

    And your post URL structure would be post-%postname%.html.

    All this to say that your tags and posts must have at least one differentiating feature in the URL structure. In this case, it's the 'post-' prefix for post URLs.

    Are you building a Wiki?