To Fix HTTP 400-499 error codes with 301 redirects in .htaccess file

To Fix HTTP 400-499 error codes with 301 redirects in .htaccess file - .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 htaccess, wordpress, mod-rewrite, url-rewriting, .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 :


This issue is on WordPress site.



I am aware we can change the URL naming convention in WP but I have changed my URL format from date format to simple postname followed by slug as shown below.



Old style:



www.example.com/2013/02/09/rin​gdroid


New style:



www.example.com/rin​gdroid


Google previously indexed my websites pages (sitemap.xml) with below format:



www.example.com/2013/04/18/hot​test-gadgets-of-2013-to-include-in-​your-list
www.example.com/2013/02/09/rin​gdroid


I have resubmitted the sitemap but there are still 404 errors in Google/Bing engine.



Could you please help me to write 301 redirects rule in .htaccess file so when someone clicks the URL for:



www.example.com/2013/02/09/rin​gdroid


They should be redirected to:



www.example.com/rin​gdroid


How we can write rule in .htaccess file to remove date part 2013/02/09/?



I tried something like this (Googled it), but it didn't work:



RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d

Solution :

RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ /$4 [R=301,NC,L]


RewriteEngine On
RewriteRule ^(d+/)3(.*)$ /$2 [R=301]


This will strip any sequence of 3 numbers each followed by slashes (so it will work with short year dates like YY/MM/DD dates or single digit days/months like 2013/2/9. If you want to keep the date in the url and have it silently redirect to the correct page without the user knowing, lose the [R=301]


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