|
#1
|
|||
|
|||
|
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 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]
[^&]+ 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]
.* 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] ? 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] 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 08:39 PM. Reason: add book link |
|
#2
|
|||
|
|||
|
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 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. |
|
#3
|
|||
|
|||
|
Quote:
![]() |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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? |
|
#6
|
|||
|
|||
|
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] |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
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] |
|
#9
|
|||
|
|||
|
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 |
|
#10
|
|||
|
|||
|
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 |
|
#11
|
||||
|
||||
|
Quote:
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:
PHP Code:
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:
Quote:
Good luck |
|
#12
|
|||
|
|||
|
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 doesn’t 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 |
|
#13
|
|||
|
|||
|
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? |
|
#14
|
|||
|
|||
|
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 |
|
#15
|
|||
|
|||
|
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] |
|
#16
|
|||
|
|||
|
Quote:
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 04:26 AM. Reason: URLs removed. |
|
#17
|
|||
|
|||
|
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] |
|
#18
|
|||
|
|||
|
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 |
|
#19
|
|||
|
|||
|
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... ![]() |
|
#20
|
|||
|
|||
|
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 |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|