.htaccess and permanent redirection with a special condition

.htaccess and permanent redirection with a special condition - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about seo, htaccess, wordpress, 301-redirect, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I have a WordPress website hosted on Bluehost which contains the following URL pattern:



http://www.example.com/2017/01/30/sample-post/


I wanted to permanent redirect it to use this:



http://www.example.com/sample-post/


So I opened .htaccess kept in the example.com folder and changed it to the following with the help of a suggestion by w3dk.



RewriteEngine On
RewriteRule ^d4/dd/dd/(.+) /$1 [R=301,L]

# 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


After I wrote this rewrite rule, everything of the URL format example.com/2017/01/30/sample-post works great. However, there are a few URLs in the website that have the format example.com/name-of-category/2008/10/20/sample-post and are throwing a 404 as the result of the above rewrite rule.



I want to permanently redirect www.example.com/name-of-category/2008/10/20/sample-post to www.example.com/name-of-category/sample-post.



What change do I need to make in my .htaccess file?


Solution :

To handle category posts of the form /name-of-category/2008/10/20/sample-post then include something like the following directive after the existing redirect.



RewriteRule ^([a-z-]+)/d4/dd/dd/(.+) /$1/$2 [R=301,L]


This would redirect /name-of-category/2008/10/20/sample-post to /name-of-category/sample-post. Note that the category consists of just the lowercase a-z and the - (hyphen), as in your example.



If you needed to allow more characters in the category then changing [a-z-] to [w-] in the above RewriteRule pattern would allow a-z, A-Z, 0-9, _ and -. But it is better to be as restrictive as possible.



So, in summary:



RewriteEngine On
RewriteRule ^d4/dd/dd/(.+) /$1 [R=301,L]
RewriteRule ^([a-z-]+)/d4/dd/dd/(.+) /$1/$2 [R=301,L]

:

Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect