Forum Moderators: phranque

Message Too Old, No Replies

Redirect Assistance :)

Code already attempted

         

bobbonew

7:42 am on May 29, 2009 (gmt 0)

10+ Year Member



Hey guys, I'm trying to redirect

[foo.com...]

to

[foo.com...]

This is what I managed to figure out, I just can't figure out having anything AFTER the first - :

RewriteRule (.*)(-)(.*)/ question.php?fn=view&id=$1&seo_name=$3

Its just not working perfectly, any suggestions?

jdMorgan

1:52 pm on May 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use a more-specific pattern to extract only the 'id' before the first hyphen.

RewriteRule ^([^\-]+)-(.+)/$ question.php?fn=view&id=$1&seo_name=$2 [L]

Also, note that this is not a redirect, it's an internal rewrite. You *do not* want to expose the dynamic path to the client with a redirect, since that would defeat the purpose of using search-engine-friendly URLs!

You will be much happier in your mod_rewrite career if you never use the wild-card subpattern ".*" unless that is the only subpattern that will do what you want to do. It is a greedy, promiscuous, ambiguous, and --when used multiple times in one pattern-- awfully inefficient subpattern to use.

The "[^\-]+" subpattern shown here matches one or more characters that are not hyphens. So, it matches everything in the requested URL-path up to the first hyphen into $1 and then stops, leaving the rest to go into $2.

Jim

bobbonew

5:25 pm on May 29, 2009 (gmt 0)

10+ Year Member



Thank you for your assistance! I'm early in my career with using regular expressions, but with this I can definitely refine it in time. I am extremely grateful.

I was thinking of doing the actual redirections with header()'s in PHP to point the old pages to the new. I guess I must have been thinking about that when I made the title. Cheers ;)

g1smd

6:00 pm on May 29, 2009 (gmt 0)

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



You can do such a redirect using the PHP HEADER function, but as Jim stated, what you likely need here is a rewrite, not a redirect.

With a rewrite, you continue to use the same old URLs out on the web even though the filenames on the server have changed.

When your script services the request, it needs to validate that $2 is the exact wording that does belong to the ID in $1. If it is not, then it needs to issue a 301 redirect to the exact correct URL. Failure to do so, allows people to link to your site like

example.com/4567-this-product-is-junk
and for your server to serve that as if it were a valid address.