Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule changing path and extension

New rewrite rule

         

mightyb

5:35 am on May 20, 2008 (gmt 0)

10+ Year Member



Hi all, first time poster, long time lurker...

I need to write the following RewriteRule, but am having no luck.

Old path on old server:

http://www.example.com/rs/2008/2/20/jaw-dropping-photosynth-demo.html

New path on new server:

http://www.example.com/2008/02/20/jaw-dropping-photosynth-demo/

Basically have to do the following:

-drop out the /rs/
-change the month from a 1 digit to a 2 digit
-Drop the ".html" at the end
-keep the post slug the same no matter the post (i.e. in this example "jaw-dropping-photosynth-demo")

Thank you for your help!

Brian

[edited by: jdMorgan at 4:40 pm (utc) on May 20, 2008]
[edit reason] example.com [/edit]

jdMorgan

3:31 pm on May 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Brian, and welcome to WebmasterWorld!

Please post your best-effort code as a basis for discussion. See our Forum Charter for links to helpful resources and information on how to get the most from this forum.

Thanks,
Jim

mightyb

3:41 pm on May 20, 2008 (gmt 0)

10+ Year Member



Thanks Jim.

I know it is something along these lines, but unsure if it is right, and what the new part should be to the right of the $

RewriteRule ^/rs/([^/]+)/\1/(.*?)(\.html)$

jdMorgan

4:56 pm on May 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest matching the fields as specifically as possible -- i.e. using number patterns where you want to match numbers.

The only real difficulty here is making the one-digit month into a two-digit month when appropriate, but that's easily solved by making the first month-digit optional in the requested URL-path.

If this code goes in httpd.conf, conf.d, or some other server-configuration-level file, then something like:


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

If the code is to go into .htaccess, drop the leading slash on the RewriteRule pattern.

Also, this new rule should precede any existing WP-provided rules in .htaccess.

As a side-note, don't put that trailing slash on the new URL unless you absolutely have to -- It's a mild violation of the way things are supposed to work, in that "/jaw-dropping-photosynth-demo" is a file, whereas "/jaw-dropping-photosynth-demo/" is a directory (or the index page in that directory, e.g."/jaw-dropping-photosynth-demo/index.php". It's a subtle thing, but worth paying attention to. Plus you save a wasted "/" character on every post request to your server.

Jim

mightyb

1:16 am on May 21, 2008 (gmt 0)

10+ Year Member



Thank you Jim, this worked perfect. I used it in .htaccess

I actually had to tweak just one part, because there were days that were also 1 digit instead of 2, so I just added a second "?"

Bri