Tuesday, 9 December 2014

404 Error while accessing categories after customizing Permalink?

I’ve been trying to customize my permalink structure since few days because the default structure of the URL’s is not at all user friendly and also it doesn’t really look good. Customizing the Permalink structure can be quite simple if you do it before you start creating pages or writing posts. 

By default the structure will be:  

http://example.com/post_id .  

You can set it to show your post name or published date or category and post name etc as per your requirement. Requirement might different depending on what exactly is your site is. Say if you have a news blog, published date might be your first choice. But for the rest of the sites like mine a URL which shows the ‘category/post’ in it, is the best choice I can think of unless you are including one post in more than one categories. If that is the case then you can include some other parameters as per your choice. However, the steps still remains the same. 

I wanted my URL’s to show the category and the post name, which will be: http://example.com/category/postname. 

For this I tried editing Permalink via ‘Permalink Settings’. Here are the steps how I did it.

1.       Go to ‘Permalink Settings’ under settings on your Left Hand Side in the WP dashboard. 

2.     Select ‘Custom Structure’. Enter the format  /%category%/%postname%/ in the text box infront of the ‘Custom Structure'.

3.    Click on ‘Save Changes’. 

4. You can see the ‘mod_rewrite’ code below the ‘Save Changes’ button. Now go and check your ‘.htaccess’ file among website files and make sure if the same code is written there. If it is not automatically written there, you can copy the same code listed here and paste it in your ‘.htaccess file’. (If you are not sure how to do it, you can refer here.

Your Permalink will now be updated and the pages should be working fine. 

However, sometimes like in my case you might get a ‘404 error’ when you go choose the category from the menu which is actually supposed to display all the posts in that category. When I clicked on individual posts, they worked fine, but not the categories. 

Later I found out this was due to some mash up in the code for detecting the category base. So I decided to edit the code. Once I disabled the code by commenting them, the issue was fixed. If you have similar issue, here are the steps how you can do it. 

1.       Install ‘Advanced Permalink’ plugin. 

2.       Once you install it, you can edit the code.  Go to ‘Advanced Permalinks’ under ‘Installed Plugin’. You see the ‘Edit ‘link under the Advanced Permalinks’ listed there. 

3.      Now search for the following code there and add ‘//’ in front of it.  

$this->add_filter ('category_rewrite_rules');

4.       Save the changes by clicking on ‘Update File’ button below. This should get your site categories up and working. 

This fixed my site. But if you have faced similar issue and have a different fix, please share with us. I'm sure there are other ways to fix this as well. Also, if you have any related issue, please share with us, so that we can together try and find a way to resolve them. 

Share this Post Share to Facebook Share to Twitter Pin This

Follow Me