1

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
1

2 Answers 2

2
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ /$4 [R=301,NC,L]
1
  • The NC flag and RewriteBase directives are not required here. This could also be "simplified" as RewriteRule ^\d{4}/\d\d/\d\d/(.*)$ /$1 [R=301,L] - no need for the additional capturing groups and this now matches the exact URL as given, no ambiguity.
    – MrWhite
    Dec 12, 2016 at 12:41
1
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]

3
  • also, is this for a wordpress site? If so it would be better to manage your url naming within the wordpress options than to go writing your own htaccess rules.
    – WebChemist
    Jun 28, 2013 at 10:09
  • 1
    Hi WebChemist , Really thankful giving an idea how to implement it. I tried your suggestion but Url its was returning as www.domain.com/ringdroid/02/09/rin​gdroid. so i have implemented my below own code and here it is. Jun 29, 2013 at 0:04
  • This regex should have worked as intended IMO, however, the missing L flag might have been the problem. The rewriting will have continued, so would have at least got caught in the WordPress "front controller" that presumably followed.
    – MrWhite
    Dec 12, 2016 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.