Forum Moderators: phranque

Message Too Old, No Replies

Can you add numbers into a redirect match?

         

madmatt69

10:50 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey all,

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]

idev

10:55 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



doesn't make any sense to me at all. your content would be moved all over with the 150% shift.

Since that's the list of posts, not the posts themselves, just permanently redirect the to the first page of the list.

madmatt69

10:57 pm on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alright - how do i do that?

idev

11:05 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



Never tested this one, so try yourself and let me know if that goes wrong.

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]

madmatt69

12:26 am on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the help.

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?

idev

1:11 am on Mar 20, 2009 (gmt 0)

10+ Year Member



as you don't have ?forum_uri in the rule, my guess is you have another rule that gets to the uri earlier. If you can't beat the htaccess monster, copy all the rules that can match that link into reply.

g1smd

10:07 am on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** It needs to be a redirectmatch though ***

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.

madmatt69

2:41 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok but maybe I'm confused - If I use mod_rewrite, doesn't that only rewrite a URL in a script or html file?

Like if I type in www.mysite.com/this-page.html directly into the browser address bar - That doesn't get re-written does it?

g1smd

2:49 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Mod_Rewrite code using a RewriteRule can be used to generate an external 301 redirect, an external 302 redirect, or a server-internal rewrite, depending on how you code the 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.

madmatt69

3:03 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alright - what about regular redirects?
like:
redirect 301 /advertising.shtml [mysite.com...]

Should those be changed to rewrite?

g1smd

3:11 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes. Change them all to use RewriteRule.

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.

jdMorgan

3:13 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because Apache processes directives on a per-module basis, and not in the order you write them in your .htaccess or config files, there is no way to guarantee the order of execution of any two directives which are handled by different Apache modules. So, since the Redirect and RedirectMatch directives are handled by mod_alias, and the RewriteRule directive is handled by mod_rewrite, you don't know --without testing-- whether the Redirects will be processed before the RewriteRules or vice-versa on any given server. In fact, if you change servers, the execution order might change.

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

madmatt69

3:15 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the clarifications! I have lots of mixed rules, and there is definitely some strange stuff going on. It'll be interesting to see what happens when I clean it up.

Also to clarify, I'm running all the rules my httpd.conf, not my htaccess - not sure if that makes a difference?

g1smd

3:27 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The same 'per module' stuff still applies.

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.

jdMorgan

4:14 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> For redirects, list those that affect very specific URLs (like one named page, or a specific extension) first, and the most general (which apply to more than one URL) last.

Both the redirects and the internal rewrites should be listed (within their two groups) from most-specific to least-specific.

Jim