Hi I am trying to move my blog from Blogger to WordPress and I want to keep a simple and clean URL structure like http://www.example.com/post-name/

In blogger, I have the following URL structure:

  1. www.example.com/2017/02/post-name.html
  2. www.example.com/2017/02/post-name.html?m=1
  3. www.example.com/2017/02/post-name.html?m=0

I can redirect all such URLs from Blogger to a clean URL on WordPress with the structure http://www.example.com/post-name/ using the redirection plugins for WordPress.

I am using the following regex expression

  • Source URL: ^/\d{4}/\d{2}/(.*).html+.*
  • Target URL: http://www.example.com/$1/

The above setup worked perfectly fine, but I want to achieve the same using the redirects server-side by editing the .htaccess file and this tool has an option to export the redirect in .htaccess format to update it on the server. But that exported rewrite rule is not working.

Here is the exported rewrite from Redirection Plugins:

# Created by Redirection
<IfModule mod_rewrite.c>
RewriteRule ^\d{4}/\d{2}/(.*).html+.* http://www.example.com/$1/ [R=301,L]
# End of Redirection

What should I do or is there any issue with my expression for .htaccess file?

1 Answer 1

 RewriteRule ^\d{4}/\d{2}/(.*).html+.* http://www.example.com/$1/ [R=301,L]

This does look as if it should "work", although there are a few things that can be tidied, and the query string (ie. m=1 or m=0) will remain on the URL.

Also, make sure you have cleared your browser cache before testing as 301s are cached hard by the browser. It can be easier to test with 302 (temporary) redirects for this reason and change to a 301 only when you are sure it's working OK. (Tip: Test with the browser's Object Inspector open and the "Disable Cache" option checked.)

This redirect also needs to go at the top of your .htaccess file, before the WordPress front-controller (ie. before the # BEGIN WordPress section).

Try the following instead:

RewriteRule ^\d{4}/\d{2}/(.+)\.html$ http://www.example.com/$1/? [R=301,L]

The trailing +.* on your pattern doesn't make a lot of sense (although it shouldn't cause a problem), as this simply repeats the l (of html) 1 or more times, followed by anything. The .html is always the end of the URL-path (the query string is not part of the URL-path), so this should really be followed by $ (end-of-string anchor).

The ? on the end of the RewriteRule substitution effectively strips the query string from the target URL by creating an "empty" query string. Alternatively, on Apache 2.4+, you can use the QSD flag instead (Query String Discard). ie. ... http://www.example.com/$1/ [QSD,R=301,L]

UPDATE: Is it safe to use ...

Well, you could be more specific with the regex, so to make sure that it doesn't match "too much" and only matches the URLs you are interested in. For example, the above directive will also match URLs of the form: example.com/2017/02/foo/bar/post-name.html, which would result in a (possibly invalid) redirect to /foo/bar/post-name. Or even example.com/2017/02/1111/22/post-name.html, which would result in a double redirect. This may not matter at all, in fact, this might even be desirable depending on your circumstances. However, you could restrict the post-name to just word characters (a-z, A-Z, 0-9 and _) and hyphens by changing the RewriteRule pattern. For example:

RewriteRule ^\d{4}/\d{2}/([\w-]+)\.html$ http://www.example.com/$1/? [R=301,L]

But otherwise, I can't see that it would be "unsafe".

  • I've updated my answer with regards to your "Is it safe" comment.
    – MrWhite
    Commented Jul 8, 2018 at 23:01
  • Thanks Mr. White, Your answer works. I have already tested and tried it.
    – nasirs
    Commented Jul 10, 2018 at 8:09

Your Answer

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

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