homepage Welcome to WebmasterWorld Guest from 54.198.130.203
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
.htaccess rewrite
can't get old url to rewrite to new location
ronnieb




msg:4274452
 7:25 am on Mar 1, 2011 (gmt 0)

Hey Guys, I have a bunch of URLs listed in the Google search and have new locations for the URLs... but don't want to lose the search position.

example

old location: articles/articles.php?article=why_drink_a_smoothie

new location: newsite/articles/why_drink_smoothie.php

I am using this rewrite rule in my .htaccess but can't get it to work right.

RewriteRule ^articles/articles\.php?article=why_drink_a_smoothie newsite/articles/why_drink_smoothie.php

Any ideas?

 

rominosj




msg:4274470
 8:55 am on Mar 1, 2011 (gmt 0)

If you need to rewrite, then use rewrite rule which allows you to use regex.

However, if you are just moving content from one directory or page to another a simple Redirect 301 should suffice.

Redirect 301 /old-page.php [domain.com...]

rocknbil




msg:4274738
 5:12 pm on Mar 1, 2011 (gmt 0)

Try escaping your ?. In a regex, it's a quantifier meaning "one or more."

^articles/articles\.php\?article=why_drink_a_smoothie

Is "newsite" a new domain or is it a directory on the current domain? If it's on the current domain, you don't need the full url.

/articles/why_drink_smoothie.php

Be sure to add the R=301, NC, and L flags for case-insensitivity and to make sure the rule stops here (L= last rule to process.) Rules following it may be taking over. All together,

RewriteRule ^articles/articles\.php\?article=why_drink_a_smoothie$ /articles/why_drink_smoothie.php [R=301,NC,L]

An aside, since you're doing this, a couple things you may consider in the interest of SEO and user friendly url's.

I use underscores all the time and while it works, this is indeed inferior to dashes. Use dashes if it's not too late. There are various reasons - just do it if you have the opportunity. :-)

Since you're redirecting, there is no reason to use extensioned destinations. Through a combination of rewrites, you can effect SEO friendly extensionless URL's. The (inefficient) way that is usually done is "if it's not a directory and it's not a file, direct to my script." This is inefficient because it requires a full file system search with each call (but is very easy, and common.)

How that would look?

RewriteRule ^articles/articles\.php\?article=why_drink_a_smoothie$ /articles/why-drink-a-smoothie [R=301,NC,L]

Then when "/articles/why-drink-a-smoothie" comes in, it would be processed like so

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /your-script.php?q=$1 [L,QSA]

where "articles/why-drink-a-smoothie" is passed to your-script.php as a query string you parse out and product the page.

[edited by: rocknbil at 5:26 pm (utc) on Mar 1, 2011]

g1smd




msg:4274745
 5:26 pm on Mar 1, 2011 (gmt 0)

Is "newsite" a new domain or is it a directory on the current domain? If it's on the current domain, you don't need the full url.

/articles/why_drink_smoothie.php

Be sure to add the R=301, NC, and L flags for case-insensitivity and to make sure the rule stops here (L= last rule to process.) Rules following it may be taking over. All together,

RewriteRule ^articles/articles\.php\?article=why_drink_a_smoothie$ /articles/why_drink_smoothie.php [R=301,NC,L]

RewriteRule cannot "see" query string data.

You need a preceding RewriteCond looking at
QUERY_STRING.

If you are telling the browser to ask for a new location you will need the [R=301,L] flags and the target URL must include the canonical domain name, otherwise non-www requests will see an unwanted two step redirection chain.

RewriteCond %{QUERY_STRING} ^article=why_drink_a_smoothie [NC]
RewriteRule ^articles/articles\.php http://www.example.com/articles/why_drink_smoothie.php [R=301,NC,L]



Then when "/articles/why-drink-a-smoothie" comes in, it would be processed like so

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /your-script.php?q=$1 [L,QSA]

where "articles/why-drink-a-smoothie" is passed to your-script.php as a query string you parse out and product the page.


This code should be banned. Period. Every request for every image, every script, every stylesheet, invokes two very slow and inefficient disk read operations.

The suggestion to go extensionless is a good one. Since you will never have extensionless "files" in the server filesystem, the rewrite for that can be optimised to one line:

RewriteRule ^([^.]+)$ /your-script.php?q=$1 [L,QSA]

Be very clear on the difference between a redirect and a rewrite.

ronnieb




msg:4274875
 8:54 pm on Mar 1, 2011 (gmt 0)

Great stuff guys. I ended up going extensionless as it seemed the cleaner, quicker way.

RewriteRule ^([^.]+)$ /your-script.php?q=$1 [L,QSA]

Also, rocknbil thanks for the tip on using hyphens instead of underscores.

g1smd




msg:4274950
 10:59 pm on Mar 1, 2011 (gmt 0)

Extensionless rocks.

It's the moment when it all sinks in that your URLs can be designed to be convenient to users, and the rewrite simply maps those requests to the server file structure, without revealing what that structure is.

You do need to add a redirect such that if someone tries to access the files at their "real" location, they are redirected to instead make another request for the correct URL.

rocknbil




msg:4275981
 4:55 pm on Mar 3, 2011 (gmt 0)

When you're right, you're right. When that code is distributed with every CMS, blog, and commerce system out there - which it almost invariably is, from WP to modX - it tends to lead to laziness, of which I'm guilty. It's what they gave us, it works . . . no excuse, off to fix as many as I can. Thanks again.

g1smd




msg:4276155
 8:24 pm on Mar 3, 2011 (gmt 0)

I'm on a mission to fix Zencart, Joomla, Drupal, Wordpress, osCommerce, vBulletin, PHPbb, and as many other forum, blog, CMS and e-comm systems as possible.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved