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

My question:

My permalink settings:

General settings> Custom > /%postname%.html

Read More

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

Related posts

Leave a Reply

3 comments

  1. 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?

  2. Hi Try this http://wordpress.org/plugins/import-html-pages/

    mports well-formed static HTML files into WordPress. Requires PHP 5.

    This plugin will import a directory of files as either pages or posts. You may specify the HTML tag (e.g. , , or ) or Dreamweaver template region (e.g. ‘Main Content’) containing the content you want to import.

    If importing pages, the directory hierarchy will be preserved. Directories containing the specified file types will be imported as empty parent pages (or, if an index file is present, its contents will be used for the parent page). Directories that do not contain the specified file types will be ignored.

    As files are imported, the resulting IDs, permalinks, and titles will be displayed. On completion, the importer will provide a list of Apache redirects that can be used in your .htaccess file to seamlessly transfer visitors from the old file locations to the new WordPress permalinks. As of 2.0, if you change your permalink structure after you’ve imported your files, you can regenerate the redirects—the file’s old URL is stored as a custom field in the imported post.