Forum Moderators: phranque

Message Too Old, No Replies

Expert mod rewrite Help Needed

         

superdevo

7:58 pm on Oct 20, 2009 (gmt 0)

10+ Year Member



Hello everyone,

I need some help.

I am trying to go from

www.somewebsite.com/page.php?artist=vincent%20van%20gogh&type=acyrlic&artworkID=955&index=0

To

www.somewebsite.com/vincent_van_gogh/acrylic_the_starry_night

I have studied this posting: www.seomoz.org/ugc/using-mod-rewrite-to-convert-dynamic-urls-to-seo-friendly-urls

And I also saw a prior posting by g1smd here (3rd reply down): www.webmasterworld.com/apache/3753656.htm

It seems like g1smd knows what he is talking about, but I have found that his code is only for 2 parameters (I need 3+) and I can't seem to figure out how to redirect the internal page (Internal Server Path: /index.php?cat=345&art=1234567) from his example to the new external page (External URL Format: www.example.com/345/1234567). When I say redirect, I mean when a user actually types in www.example.com/index.php?cat=345&art=1234567 then they are automatically redirected to the www.example.com/345/1234567.

Also how would I incorporate g1smd's example with one that will allow me to re-use the dynamic PHP parameters at the end of my current URL (?artist=vincent%20van%20gogh&type=acyrlic&artworkID=955&index=0)?

Maybe I am asking the wrong questions or too many closed ended questions to find a solution. I really just need to know how to get from this

www.somewebsite.com/page.php?artist=vincent%20van%20gogh&type=acyrlic&artworkID=955&index=0

To this

www.somewebsite.com/vincent_van_gogh/acrylic_the_starry_night

in the most proper and efficient way possible, so that I am re-writing correctly, not creating duplicate content in any form or fashion, and can still access and reuse my PHP parameters after the rewriting and redirection takes place.

Any and all help will be greatly appreciated.

Thank you

TheMadScientist

9:29 pm on Oct 20, 2009 (gmt 0)

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



Hi superdevo,

Welcome to WebmasterWorld!

The best solution I have found for this type of situation is to Rewrite your URLs with the QUERY_STRINGS to a PHP page which can then easily connect to your DB, pull your current URLs apart and Redirect to the correct location. You can then switch back to Mod_Rewrite to serve the information to the new location using a PHP page.

# I'm using artist= to ensure there is a correct match.
# It may or may not be necessary in your situation...

RewriteCond %{QUERY_STRING} ^artist=([^.]+)
RewriteRule ^page\.php$ /redirect_em.php?artist=%1 [L]

# End Mod_Rewrite

/* redirect_em.php below */

$make_new_URL = explode("&",$_SERVER['QUERY_STRING']);

/* Connect to DB and restructure the URL here */

header( "HTTP/1.1 301 Moved Permanently" );
header("Location: http://www.example.com/the_new_URL");

# Back to Mod_Rewrite

RewriteRule ^new_url/(extensionless)/(info_here)/(more_here)$ /the_page_with_the_info.php?var=$1&var2=$2&var3=$3 [L]

NOTE: You could theoretically just adjust page.php to do the redirect...

TheMadScientist

10:05 pm on Oct 20, 2009 (gmt 0)

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



I think it's a bit too late to add to my previous post, but you could use $_GET rather than QUERY_STRING, depending on your exact situation and coding style... (There's a bunch of different ways to arrive at the desired result and which is best or most efficient depends on you and your exact situation.)

Hope I gave you some ideas anyway.

superdevo

6:32 am on Nov 17, 2009 (gmt 0)

10+ Year Member



MMadScientist,

Thank you for the welcome.

You guys are pretty awesome here and I can't believe I went so many years as a web developer ignoring the use of the Webmaster World forum.

I do have a few questions though.......

What exactly goes in place of "new_url/(extensionless)/(info_here)/(more_here)$ /the_page_with_the_info.php"

IN THE SECTION

# Back to Mod_Rewrite

RewriteRule ^new_url/(extensionless)/(info_here)/(more_here)$ /the_page_with_the_info.php?var=$1&var2=$2&var3=$3 [L]

ALSO,

1) Do you have any tips on cleaning the DB information that is pulled to create the URL?

2) Tips for retrieving the DB variables after the page is rewritten?

2) What code do I add in the process to handle the relative links to image, CSS, and external JS that may be in the page......I heard this can usually be a common issue?

Thanks

jdMorgan

1:31 pm on Nov 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> What code do I add in the process to handle the relative links to image, CSS, and external JS that may be in the page......I heard this can usually be a common issue?

See recent thread: [webmasterworld.com...]

The problem that you face with this project is that the 'friendly' and 'unfriendly' URLs are no longer just re-arrangements of each other -- For example, your query-string-format internal filepath does not contain "starry night" and your friendly URL does not contain the "id number" -- These are 'associated' values, but a database lookup is required to convert either one to the other.

So as TheMadScientist says, the solution is to use very simple rewriterules to 'send' requests that need to be redirected or rewritten to a/your script, and let it look up the database record and get the 'conversion' values needed.

PHP's preg_replace directive is quite handy for 'cleaning up' and formatting database entries for use in building redirect URLs or script filepaths.

When using this 'script' method, the redirect function in PHP is pretty much the same as in mod_rewrite: You output the "Location" header containing new URL, and output a server status response of 301-Moved Permanently. However, replacing the internal rewrite function may not be so obvious; The PHP equivalent of a mod_rewrite internal rewrite is to 'include' the target file 'inside' your rewriting script and output its contents directly to the client.

Jim