Search Engine Watch
SEO News

Go Back   Search Engine Watch Forums > Search Engine Marketing Strategies > Search Engine Optimization > Dynamic Website and Technical Issues
FAQ Members List Calendar Forum Search Today's Posts Mark Forums Read

Reply
 
Thread Tools
Old 01-27-2005   #1
seomike
Md_Rewrite Guru
 
Join Date: Jun 2004
Location: Dallas, Texas but forever a Floridian!
Posts: 627
seomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to behold
Mod rewrite tips and tricks

Tip 1
instant 301 from old dynamic url to new static url
(will only work if you use the exact variables from the dynamic url)

Old dynamic url:
Code:
www.somesite.com/catalog.php?cat=widgets&product_id=1234
New static url:
Code:
www.somesite.com/catalog/widgets-1234.html
Code:
#start .htaccess code
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^cat\=([^&]+)\&product_id\=([^&]+)$
RewriteRule ^$ /catalog/%1-%2.html [R=301,L]
Reg Expression notes:
[^&]+ mean find any character except the "&" since it is what seperates the variables in a string. you can back reference matches in a rewrite condition using ()'s just like your rewrite rules but to call them you have to use a % instead of a $.

Benefits:
1. You don't have to hand write 1,000's of 301 redirects

2. Spiders can easily pick up the 301 and pass the scores and index the new urls much faster than having to crawl the entire site over from scratch.

3. Users clicking old dynamic urls in the SERPS will not get a 404 error. They'll go straight to the new static urls.

Tip 2

Change a product name or change a mispelling and you've just lost all page scores to the static mod rewritten url. Unless...

Example url:
www.somesite.com/dinnerplates/cassa-stone/
Should be spelled
www.somesite.com/dinnerplates/casa-stone/

Code:
RewriteEngine On
RewriteBase /

# detect if the file being requested is a mispelling
RewriteCond %{REQUEST_URI} /.*cassa.*$
#redirect to the correct spelling using a 301
RewriteRule ^(.*)cassa(.*)$ /$1casa$2 [R=301,L]
Reg Expression notes:
.* means match any character 0 to infinite times. so .*cassa.* so zzzzcassazzzz would be a match as well as /cassa-stone/. All I'm doing is back referencing everything before and after cassa and then copying it into the new url and doing a redirect.

Benefits
This can really save your @$$ nuff said.

Tip 3

Make the unormalized, normalized for Yahoo!'s sake.

Example code:
<a href="some-directory/">some directory</a>

All bots and SE's are different. ugh. Google is very good at indexing and scoring exactly what is in the <a href> tag whether the url is normalized or not. Yahoo on the other hand is a stickler for normalization and will index the example above like so in there SERPS http://www.somesite.com/some-directory

to keep a 404 from kicking in if some-directory needs to be picked up by your mod rewrite make sure you end all your RewriteRules that end their mathcing on directory/folder like so.

Code:
RewriteRule ^([^/]+)/?$ /somepage.php?name=$1 [L]
who thought a question mark would ever be an answer
? mean match the previous character 0 to 1 times. and will fix any normalization problems you might have encountered with Y!.

Code:
#This will send you into a horrible loop and bad thing may happen :(
#so don't use this
RewriteRule ^(.*)$ /somepage.php?name=$1 [L]
Benefits
1. users will not hit a 404 when clicking a yahoo SERP

2. worth its weight in gold!

3. Your site will not look like a bad cloak hehe

The Definitive Guide to Apache mod_rewrite

Last edited by AussieWebmaster : 03-02-2010 at 09:39 PM. Reason: add book link
seomike is offline   Reply With Quote
Old 01-27-2005   #2
Robert_Charlton
Member
 
Join Date: Jun 2004
Location: Oakland, CA
Posts: 743
Robert_Charlton has much to be proud ofRobert_Charlton has much to be proud ofRobert_Charlton has much to be proud ofRobert_Charlton has much to be proud ofRobert_Charlton has much to be proud ofRobert_Charlton has much to be proud ofRobert_Charlton has much to be proud ofRobert_Charlton has much to be proud ofRobert_Charlton has much to be proud of
seomike - Great post... Thanks.

The code for your "old dynamic url" gets compressed by the bbs into a shortened version that's hard to read. Here it is as code....

Code:
http://www.somesite.com/catalog.php?cat=widgets&product_id=1234
I find regular expressions really hard, particularly since I don't use them that often, but I've found I can understand what's going on if they're explained line by line.

Are you game for that? It's a lot of work, I know, and may be beyond the scope of what you intended to do.
Robert_Charlton is offline   Reply With Quote
Old 01-27-2005   #3
seomike
Md_Rewrite Guru
 
Join Date: Jun 2004
Location: Dallas, Texas but forever a Floridian!
Posts: 627
seomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to behold
Quote:
Are you game for that?
I added some notes to the post above
seomike is offline   Reply With Quote
Old 02-10-2005   #4
hooter
Newbie
 
Join Date: Aug 2004
Posts: 4
hooter is on a distinguished road
First Mike,

Excellent tutorial - problem is I'm as dense as a brick when it come to deciphering what \$* goes in where or comes out there to accomplish what I'm after. Here is an example of what my old dynamic URLs in SERPS look like:
product.php?productid=60&cat=1&page=1
home.php?cat=12

With my mod_rewrite rules and altering my application code I can now have static URLs like:
ProductName-pr-60.html
CategoryName-c-12.html
where pr is product id and c is category number. Using these as an example, how would I do what you did to 301 redirect those in that dynamic format into the static example format?

I realize I don't have the Product or Category name embedded in the original dynamic URLs but if I could just redirect to something generic that would still work I think?
example: Product-pr-60.html or Category-c-12.html

Here are my rewrite rules which might help understand what redirect patterns would/might work:
RewriteRule ^(.*)-p-([0-9]*)-pr-([0-9]*)(.*).html$ product.php?page=$2&productid=$3&$4
RewriteRule ^(.*)-pr-([0-9]*)(.*).html$ product.php?productid=$2&$3
RewriteRule ^(.*)-c-([0-9]*)-p-([0-9]*).html$ home.php?cat=$2&page=$3
RewriteRule ^(.*)-page-([0-9]*).html$ pages.php?pageid=$2
RewriteRule ^(.*)-c-([0-9]*).html$ home.php?cat=$2

Or would it just be safer to redirect all of these dynamic to my default home.php (all my new rewrite static URLS can be followed from there)

Many thanks in advance.
hooter is offline   Reply With Quote
Old 03-25-2005   #5
nex99
 
Posts: n/a
Dear Mike,

At first, Thanks for your brilliant post.

i wants to use mod_redirect rule to change :

http://www.somesite.com/v_b2b.php?ca...=sale&start=50
To
http://www.somesite.com/v_b2b/sale-50.html

Code Used :

#start .htaccess code
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^category\=([^&]+)\&start\=([^&]+)$
RewriteRule ^$ /v_b2b/%1-%2.html [R=301,L]


i use the exact code in my .htaccess but seems change nothing. is their anything wrong?
  Reply With Quote
Old 03-25-2005   #6
seomike
Md_Rewrite Guru
 
Join Date: Jun 2004
Location: Dallas, Texas but forever a Floridian!
Posts: 627
seomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to behold
Hey Nex99 welcome to the forums.

Change your code in the pages to out put a static url. Mod rewrite just translates the static back its dynamic form.

Then all you need is this rule

#start .htaccess code
RewriteEngine On
RewriteBase /

RewriteRule ^v_b2b/([^/]+)-([^/]+)\.htm$ /v_b2b.php?category=$1&start=$2 [L]
seomike is offline   Reply With Quote
Old 04-15-2005   #7
pintofmilk
Newbie
 
Join Date: Apr 2005
Posts: 2
pintofmilk is on a distinguished road
Created a Loop?

Hi Mike,

Great article... using it I have finally conquered the mod_rewrite problem that I has been on my todo list forever!

As well implementing mod_rewrite to translate static HTML URLs to dynamic PHP ones, I want to eliminate the dynamic URLs from the search engines. I cannot quite get it working though!

This is my working static to dynamic rule:
RewriteRule ^r/accom_one/(.*)\.html /r/accom_one.php?a=$1

And this is what I am trying to get working to eliminate the dynamic URLs from the search engines:
RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^r/accom_one\.php /r/accom_one/%1.html [R=301,L]

When I put the second bit in the file with the first bit, all the accom_one pages fail - static and dynamic. Have I created a mod_rewrite loop or just made a simple mistake?

Thanks,
Ben
pintofmilk is offline   Reply With Quote
Old 04-15-2005   #8
seomike
Md_Rewrite Guru
 
Join Date: Jun 2004
Location: Dallas, Texas but forever a Floridian!
Posts: 627
seomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to behold
I'm glad that the tips helped you out! It's great to hear.

As per your rewrite rule try this


RewriteCond %{QUERY_STRING} ^a=([^&]+)$
RewriteRule ^.*$ /r/accom_one/%1.html [R=301,L]
seomike is offline   Reply With Quote
Old 04-15-2005   #9
pintofmilk
Newbie
 
Join Date: Apr 2005
Posts: 2
pintofmilk is on a distinguished road
Thanks Mike,

But won't that rule try to match any URL where the variable 'a' has been set? I only want it to operate on accom_one.php pages and not any others...?

Thanks,
Ben
pintofmilk is offline   Reply With Quote
Old 04-15-2005   #10
alfo
Member
 
Join Date: Apr 2005
Posts: 5
alfo is on a distinguished road
Smile

Hi all,

Firstly this is just what I have been looking for, this is the best instructions I have found, thanks!

I am afraid I am on the first step to try and understand this so please be patient with me. Just a few simple questions I am sure to most here would know the answers to.

1) I wish to try and change the url http://www.mysite.com/proddetail.php?prod=PRODUCT-1 to http://www.mysite.com/PRODUCT-1.html
I have hacked and chopped but can't get the new static page to show in the browser.

2) If I create a sitemap with the new static links would that be enough or would every link on the site need to point to the new static pages?

3) Do I need to do anything to avoid SE finding double content .php/.html?

Sorry if these questions all seem a bit simplistic.

Thanks so much for a fantastic posting.
alfo
alfo is offline   Reply With Quote
Old 04-16-2005   #11
seomike
Md_Rewrite Guru
 
Join Date: Jun 2004
Location: Dallas, Texas but forever a Floridian!
Posts: 627
seomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to behold
Quote:
But won't that rule try to match any URL where the variable 'a' has been set? I only want it to operate on accom_one.php pages and not any others...?
RewriteCond %{REQUEST_URI} ^.*accom_one\.php
RewriteCond %{QUERY_STRING} ^a=([^&]+)$
RewriteRule ^.*$ /r/accom_one/%1.html [R=301,L]

Pintofmilk this extra condition will limit it to the accom_one.php file Good luck!

Quote:
1) I wish to try and change the url http://www.mysite.com/proddetail.php?prod=PRODUCT-1 to http://www.mysite.com/PRODUCT-1.html
I have hacked and chopped but can't get the new static page to show in the browser.
What you need to look for is the echo or print functions that out put the variables in the coding. something like this

PHP Code:
echo '<a href="somefile.php?var='.$db_val['column_name'].'">...</a>';
If you can locate where the links are generated in the code then you can just change the structure in the href="" to make it out put a static looking url.

If you can do that then you just need to add this to your .htaccess file in the root of your website on the server

Code:
#htaccess mod rewrite code
RewriteEngine On
RewriteBase /

#code to tell spiders where your new page is
RewriteCond %{QUERY_STRING} ^prod=([^&]+)$
RewriteRule ^.*$ /%.html [R=301,L]

#code to write product-1.html back to proddetail.php?... on the server
RewriteRule ^([^/]+)\.html$ /proddetail.php?prod=$1 [L]
Quote:
2) If I create a sitemap with the new static links would that be enough or would every link on the site need to point to the new static pages?
It would actually be enough, but you'd have duplicate pages. If you can pull off the code change to output a static url, plus a site map and then 301 all versions of the old dynamic urls to the static ones you'll be cooking with some serious gas!

Quote:
3) Do I need to do anything to avoid SE finding double content .php/.html?
Yes, use Tip 1 that will tell the spider that the old dynamic url is now permantly changed to the new static one (I wrote it in the htaccess code above also)

Good luck
seomike is offline   Reply With Quote
Old 04-16-2005   #12
alfo
Member
 
Join Date: Apr 2005
Posts: 5
alfo is on a distinguished road
Hi Mike

Thank you so much for your help! This is what I have found so far

$startlink='<a href="proddetail.php?prod=' . urlencode($rs["pId"]) . (@$catid != "" && @$catid != "0" && $catid != $rs["pSection"] ? '&cat=' . $catid : "") . '">';

I have tried the following, the link looks good in the browser i.e. http://www.mysite.com/PRODUCT-1.html but the page never loads (the browser doesnt timeout either).

$startlink='<a href="' . urlencode($rs["pId"]) . (@$catid != "" && @$catid != "0" && $catid != $rs["pSection"] ? '&cat=' . $catid : "") . '.html">';

Do you know what I might be doing wrong?

Many thanks for your time,
alfo
alfo is offline   Reply With Quote
Old 05-04-2005   #13
madgett
 
Posts: n/a
mod_rewrite redirect method for 2 diff servers

How could I accomplish this on a redirect request from .htaccess:

http://www.server1.com -> http://12.23.34.45/dir/dir/dir

But it should still display in the address bar: http://www.server1.com

Anybody have any ideas?
  Reply With Quote
Old 06-18-2005   #14
fazman
Member
 
Join Date: Jun 2005
Location: Aus
Posts: 5
fazman is on a distinguished road
Hi Guys, Im new to this forum. Just been reading over this thread and it seems Ive come to the right place.

Im looking for a way to link to say

http://www.domain.com/order/PH6 and have the PH6 become a variable to be used in a php script.

I am pretty sure I can do it with the mod rewrite rule in htaccess to change it from

http://www.domain.com/order/PH6 to http://www.domain.com/order/?order=PH6

If I can do this with the modrewrite rules then I can break it back down and let my script do the rest.

Ive noticed that everyones question have sort of been the opposite way to what Im after. Changing the variable to a static url, where I want to change the static url to a variable.

Does anyone have any thoughts on how to do this.

Thanx

Fazman
fazman is offline   Reply With Quote
Old 06-18-2005   #15
phpmaven
And now for something completely different...
 
Join Date: Jan 2005
Location: San Clemente, California
Posts: 71
phpmaven will become famous soon enough
You will need to change your approach slightly to avoid having having Apache go into an infinite loop, but something like the following should work.

RewriteEngine on
RewriteBase /

RewriteRule ^order/(.*) /order.php?order=$1 [L]
phpmaven is offline   Reply With Quote
Old 06-18-2005   #16
fazman
Member
 
Join Date: Jun 2005
Location: Aus
Posts: 5
fazman is on a distinguished road
Quote:
Originally Posted by phpmaven
You will need to change your approach slightly to avoid having having Apache go into an infinite loop, but something like the following should work.

RewriteEngine on
RewriteBase /

RewriteRule ^order/(.*) /order.php?order=$1 [L]

Hi phpmaven. thanx for the fast reply. I tested that out. Im still getting a 404 error when i try it out though.

If you would like to see what I mean, it should go from

<snip>

the second url would then change the header location to where that variable tells it to.

Hopefully the urls are not offensive.

Also is there anything else I need in my htaccess file other than this

RewriteEngine on
RewriteBase /

RewriteRule ^order/(.*) /index.php?order=$1 [L]

Thanx

Fazman

Last edited by Marcia : 06-19-2005 at 05:26 AM. Reason: URLs removed.
fazman is offline   Reply With Quote
Old 06-20-2005   #17
seomike
Md_Rewrite Guru
 
Join Date: Jun 2004
Location: Dallas, Texas but forever a Floridian!
Posts: 627
seomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to behold
Here is the best way to do it. Using the /?$ will catch the url with or without the trailing slash

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQEUST_URI} ^/order.*
RewriteRule ^order/([^/]+)/?$ order/?order=$1 [L]
seomike is offline   Reply With Quote
Old 06-21-2005   #18
fazman
Member
 
Join Date: Jun 2005
Location: Aus
Posts: 5
fazman is on a distinguished road
Hi Mike, thanx for that. I tested it out in both the root directory and the order/ directory with the .htaccess files. Neither worked. It still seems to assume that the order/PH6 is another directory, when its supposed to work out the PH6 is actually a variable.

I know that the mod rewrite rule works on my server as I use it on some other domains I needed it on. This one is just frustrating me. I only wanted to do it this way for those search engines out there that dont follow variables that use the ?.

What you have given me makes sense to me, and looks like it should do it, but for some reason I just keep coming up with a 404 page from it. I even tried to change the

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQEUST_URI} ^/order.*
RewriteRule ^order/([^/]+)/?$ order/?order=$1 [L]

TO

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQEUST_URI} ^/order.*
RewriteRule ^order/([^/]+)/?$ order/index.php?order=$1 [L]

to see if adding the actual filename in would make a differance. It didnt.

It just needs to go from domain.com/order/PH6 to domain.com/order/index.php?order=PH6

Anymore thoughts would be greatly appreciated

Thanx

faz
fazman is offline   Reply With Quote
Old 06-21-2005   #19
seomike
Md_Rewrite Guru
 
Join Date: Jun 2004
Location: Dallas, Texas but forever a Floridian!
Posts: 627
seomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to beholdseomike is a splendid one to behold
Try this

RewriteEngine On
RerwiteBase order/
RewriteCond %{REQUEST_URI} ^/order.*
RewriteRule ^order/([^/]+)/?$ index.php?order=$1 [L]


if that doesn't work

strip of the conditions and just use the rule with the full url filled out.

#test to see if the matching is even working at all
RewriteEngine On
RewriteRule ^order/(PHP6)/?$ index.php?order=$1 [L]

if that doesn't work then there is something up with the modrewrite configurations...
seomike is offline   Reply With Quote
Old 06-22-2005   #20
fazman
Member
 
Join Date: Jun 2005
Location: Aus
Posts: 5
fazman is on a distinguished road
Thanx again Mike.

I got the first one uploaded, had to fix a spelling mistake. It returned me a 500 server error. Just checked the error log and it says

.htaccess: RewriteBase: argument is not a valid URL

The second one came up with a url not found

Getting closer now though I think, especially since I got a 500 error. Its something differant

faz
fazman is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off