Forum Moderators: phranque

Message Too Old, No Replies

Redirect dates to non-dated URLs

         

emor8t

12:58 pm on Oct 14, 2010 (gmt 0)

10+ Year Member



So, I did spend some time poking around trying to find the answer to my question on this forum and Google, but I'm new to this so I need it spelled out a bit more than what I've found. I can't imagine this hasn't come up before.

I have changed the way I'd like to do URLs on my blog and I'm wondering if there is a way to do this with one rewrite rule, rather than one for every post.

I'd like to redirect www.example.com/2010/12/some-blog-post/
to www.example.com/some-blog-post/

jdMorgan

1:47 pm on Oct 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is simply a specific example of redirecting a "deep subdirectory" URL to a shorter, shallower subdirectory URL.

Use the pattern-matching power of regular expressions to detect "/<four digits>/<two digits>/", create a back-reference to everything that follows the URL path-part matching that pattern, prepend the protocol and hostname to that back-reference, and 301-redirect to the resulting URL.

You can use either RewriteRule (mod_rewrite) or RedirectMatch (mod_alias) to accomplish this with a single directive. However, this pre-supposes that you've already got other working redirects, and don't need help with "set-up" directives. (Note that to avoid problems due to unknown or potentially-changing execution order, you should not mix directives from both of these modules: If you use mod_rewrite for anything, use it for everything.)

If you have problems implementing that, please post your best-effort code as a basis for discussion, so we have some idea of where to start. The resources in our Apache Forum Charter and the examples and tutorials in our Apache Forum Library may prove useful in this regard. Links to both are at the top of every page.

---

That answers your direct question. However, another question that should be asked before starting is, "What are you actually trying to accomplish here?" This redirection *will not* change the actual URLs, because they are defined by the links that you publish on your HTML pages. So the above answer is probably only part of the last --and optional-- step of a three-step process.

The first step is to edit your HTML pages or to modify the script(s) that produce them, so that you "publish" the new and shorter URLs as links on your pages. These links define and create the URLs. If this is not feasible for you, then a purchased "SEF plug-in" solution will be required.

The second step is to create a URL-to-filepath rewriting rule to 'map' requests for the new/shorter URLs to your script(s). This rule may in fact already exist; These rules are usually supplied to users by CMS providers (and they're usually badly-coded, which is another subject -- see our Library).

The final -and optional- third step is a special case of the direct answer above: You can slightly speed up search engines' re-indexing of your site and preserve traffic and/or "link juice" from old third-party links and obsolete bookmarks by redirecting only direct client requests for the old URLs to the new ones.

Anyway, there are no shortcuts, and the first two steps will be required before proceeding to the third.

Jim

sublime1

2:31 pm on Oct 14, 2010 (gmt 0)

10+ Year Member



Everything Jim says: right on.

As one thing to look at: it looks like your URLs are the kind put out by WordPress or Drupal (or other) blogging platforms. If so, both have a means of changing the format of the links that are output, and both also can be configured to do the necessary redirects so old URLs keep working. Chances are, your existing blogging software can do all of the work without ever touching .htaccess. Not touching .htaccess is a Good Thing :-)

Tom