Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite -- almost there...

         

compooter

7:12 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



I'm changing a naming convention for some files on my server and am trying to accommodate for old file requests. The old versions included version numbers in the filenames, but the new ones are under version control so the version numbers are being removed from here on.


Example:
abc_foo-v0.2.1.php -> abc_foo.php
abc_foo_bar-v0.1.php -> abc_foo_bar.php

The naming convention is fairly straightforward. There is either one or two [a-z] name modifiers after the initial 'abc' prefix. The version number is always pretty much v0.x or v0.x.x

Here is the rule I have so far:

RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^/sandbox/plugins/(abc_[a-z]+)(_[a-z]+)?(\-v0\.[0-9]+)(\.[0-9])?\.php$ /sandbox/plugins/$1$2.php [R=301,L]

However, as of yet the redirect isn't working. I know it's close... any ideas?

compooter

9:51 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



Oh, btw - you can ignore the RewriteCond. That is just to override the effect of another rewriterule set.

compooter

10:19 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



Ok, I've simplified it a bit but still no luck:

RewriteRule ^sandbox/plugins/(abc[a-z_]+)(.+)\.php$    /sandbox/plugins/$1.php [R=301,L]

jd01

10:32 pm on Jun 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi compooter,

If I am understanding correctly, it looks like you should be able to accomplish your goal with a forward looking negative regular expression. This should be more efficient than trying to find a specific match, because we can 'look ahead' and break when there is not a match, like this:

RewriteRule ^sandbox/plugins/([^-]+)[-]v /sandbox/plugins/$1.php [R=301,L]

By changing to the forward looking expression, we can store anything that is not a - in a variable, then by requiring the -v for a match, we can eliminate looping of your new file (the v appears to be overkill, but if any of the files are - something-else may be required). Normally, I recommend a hard ending ($) on the left side of the rule, but in this case, leaving it off allows us to use the implicit 'and anything else' so we do not have to check for an exact match, because if we match this far, the information need is already stored, and files that should not be rewritten (should) have been eliminated by not matching the -v pattern.

Hope this helps.

Justin

Added: [] on the - are really optional, I use them to keep things straight in my head sometimes =)

compooter

10:48 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



Ok that caused a little recursion, so here's the final version:

RewriteRule ^sandbox/plugins/(abc[a-z_]+)-(.+)\.php$ /sandbox/plugins/$1.php [R=301,L]

compooter

10:51 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



Ah very nice. I like that solution - thanks!