Wordpress custom permalinks using .htaccess

2019-04-06 20:41发布

Currently I added custom structure for posts premalinks /post/%post_id% through permalinks settings in Admin.

Since I need to rewrite permalinks for categories, tags and author pages as follow:

Categories

Current /post/category/category_name to /category_name

Tags

Current /post/tag/tag_name to tag/tag_name

Author

Current /post/author/author_username to /author/author_username

I tried to create custom RewriteRule in .htaccess, which didn`t work at all:

RewriteRule ^/([^/]*)$ ./post/category/$1 [L]
RewriteRule ^/tag/([^/]*)$ ./post/tag/$1 [L]
RewriteRule ^/author/([^/]*)$ ./post/auhtor/$1 [L]

Any help with .htaccess rules coding to achieve such permalinks is much appreciated.

4条回答
何必那么认真
2楼-- · 2019-04-06 21:00
  1. Make sure you have rewrite engine on in your apache configuration
  2. Login to your WordPress with administrative rights and go to Settings -> Permalinks as shown into attached three images. enter image description here
  3. If permalink is not updated, then you don't have write access to root of website or rewrite module of apache is not enabled.
  4. If permalink message appears, it means it also created .htaccess in the root of WordPress site.

This way you can achieve following url formats:

Categories

category/category_name

Tags

tag/tag_name

Author

/author/author_username

Note: it is always best practice to prefix url so for category, /category prefix should be added into path rather than directly accessing it because wordpress have page and post which are accessed directly without any prefix generally.

查看更多
Root(大扎)
3楼-- · 2019-04-06 21:11

I didn't test with WordPress, but with http://htaccess.mwl.be/

You were pretty close:

RewriteRule ^([^/]+)$ /post/category/$1 [L]
RewriteRule ^tag/([^/]+)$ /post/tag/$1 [L]
RewriteRule ^author/([^/]+)$ /post/author/$1 [L]
查看更多
放我归山
4楼-- · 2019-04-06 21:11

I didn't test with WordPress, but with http://htaccess.mwl.be/

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/([^/]*)$ ./post/category/$1 [L]
RewriteRule ^/tag/([^/]*)$ ./post/tag/$1 [L]
RewriteRule ^/author/([^/]*)$ ./post/auhtor/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
查看更多
做个烂人
5楼-- · 2019-04-06 21:16

If you are not doing a pretty high-performance Project, i would strongly suggest noch to mess around with the Wordpress standard .htaccess and instead use Wordpress internal functions to accomplish the same task. Despite a cleaner Project this would also make your solution Webserver agnostic (who knows if you want to switch to NGINX or something else in the future).

Wordpress main function that should help you quite a bit is add_rewrite_rule. As an example your category link could look like that:

  // place this rule inside function.php
  add_action("init", function() {
      add_rewrite_rule('^([^/]+)/?', 'index.php?category_name=$matches[1]', 'top');
  });

Though you should bear in mind, that the regex should have exceptions for other 'top-level' objects.

An important note from the wordpress docs: After adding new rules you have to flush the rules table: From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

There is also a plugin called Rewrite available. As it has not been updated for a long time, i would not suggest using it in Production, but its code could server as a source of inspiration.

查看更多
登录 后发表回答