Forum Moderators: phranque
I'm trying to create a redirect match-
Basically I have urls like this:
http://www.example.com/forums/this-new-page-fmp3-10.html
Where the "-10" goes up in multiples of 10 so for example the next link would be:
http://www.example.com/forums/this-new-page-fmp3-20.html
Now here's what the new URL's look like:
http://www.example.com/forums/this-new-page-vf3-15.html
So the changes are "fmp" turning into "vf" and "-10" turning into "-15" - it has now changed to multiples of 15 instead of multiples of 10.
Can anyone help me with a redirect match rule for this?
I already tried this as a test:
RedirectMatch 301 ^/forums/this-new-page-fmp9-([0-9]+)\.html http://www.example.com/forums/this-new-page-vf9-$1.html
And it didn't work - I'm sure the final rule needs to be more complicated as well.
Can anyone help me out with this? Much appreciated!
[edited by: madmatt69 at 10:55 pm (utc) on Mar. 19, 2009]
[edited by: jdMorgan at 10:59 pm (utc) on Mar. 19, 2009]
[edit reason] example.com [/edit]
assuming all the links go to
http://www.example.com/forums/this-new-page-vf3.html
you can turn it to
http://www.example.com/forums/this-new-page-vf3-0.html
or whatever the first page is.
RewriteRule /forums/this-new-page-fmp3-[0-9]+\.html http://www.example.com/forums/this-new-page-vf3.html [L,R=301]
if the number after the fmp is dynamic, go with this one:
RewriteRule /forums/this-new-page-fmp([0-9]+)-[0-9]+\.html http://www.example.com/forums/this-new-page-vf$1.html [L,R=301]
It needs to be a redirectmatch though - The Rewrite Rule doesn't work, because the links coming into those pages are from a different site that I don't control.
I tried making this redirect match:
RedirectMatch 301 ^/forums/[a-z0-9_-]*-fmp([0-9]+)-[0-9]+\.html$ [mysite.com...]
Unfortunately that results in a URL looking like this:
[mysite.com...]
I think I'm goofing something up, and as a result it's being sent to a different rewrite rule.
It's probably to do with where I have $1 and $2 setup, as I have no idea what I'm doing with those - I'm just sort of copying a similar rule I already have in place.
Suggestions on how to clean it up?
No!
Do not mix rules using Redirect/RedirectMatch and rules using RewriteRule inside the same .htaccess file. If you do mix them, you cannot guarantee which order they will be evaluated.
Failure to do that can often mean that redirects are activated after internal rewrites - and that exposes internal filepaths back out into the URL. That may be what is happening here.
All the rules from one Apache module are evaluated before the rules using the other Apache module, irrespective of which order they appear in your .htaccess file.
Use Mod_Rewrite code for all of your rules.
A redirect takes an external URL request and tells the browser it needs to make a new HTTP request for a new URL.
A rewrite takes a URL request and alters the internal pointer so that the content is fetched from a different internal server filepath to the one suggested by the path part of the original URL request.
A 301 redirect is coded with full domain name in the target URL and [R=301,L].
If you include the domain name, and then either omit the R flag, or omit the number from the [R] flag, then you get a 302 redirect.
If you omit both the domain name and the R flag, then you get an internal server rewrite.
All rules, redirects and rewrites, should end with [L] too.
Should those be changed to rewrite?
Do not mix rules using Redirect/RedirectMatch and rules using RewriteRule inside the same .htaccess file. If you do mix them, you cannot guarantee which order they will be evaluated.
Failure to do that can often mean that redirects are activated after internal rewrites - and that exposes internal filepaths back out into the URL. That may be what is happening here.
All the rules from one Apache module are evaluated before the rules using the other Apache module, irrespective of which order they appear in your .htaccess file.
Use Mod_Rewrite code (i.e. RewriteRule) for all of your rules.
This can lead to "stacked" or "chained" redirects, which can confuse search engine spiders, or it can "expose" internal filepaths to clients if an internal rewrite is executed before an external redirect.
So as a general rule of thumb, it's a good idea to use only mod_rewrite for all redirects and internal rewrites, if you use it for any redirects or rewrites.
Jim
Clean them all up and look carefully at the order they are listed in.
List all of the redirects before you list any of the rewrites.
For redirects, list those that affect very specific URLs (like one named page, or a specific extension) first, and the most general (like 'all non-www') last.
Ensure that every redirect contains the full domain name, and R=301.
Make sure that all rules, both redirects and rewrites, have [L] at the end.
Both the redirects and the internal rewrites should be listed (within their two groups) from most-specific to least-specific.
Jim