View Full Version : Mod_Rewrite, quick and easy
currentstyle
25-02-09, 13:15
Hi, I've just created a quick and easy mod_rewrite fix, for V2 Shopwindow Client here it is, but, remember, backup your files first, just in case it doesn't achieve the desired results. this works on Apache/Linux/Mod_Rewrite switched on
I've just updated this to remedy a couple of issues, the pagination is still ugly, but the rest of it works much better now
In constants.inc.php
find:
define('P_GOTO', HOME_URL.'goto.php');
define('P_INDEX', HOME_URL.'index.php');
define('P_PRODUCT', HOME_URL.'product.php');
define('P_PRODUCTLIST', HOME_URL.'productlist.php');
define('P_CATEGORY', HOME_URL.'category.php');
replace with:
define('P_GOTO', HOME_URL.'goto');
define('P_INDEX', HOME_URL.'index.php');
define('P_PRODUCT', HOME_URL.'product');
define('P_PRODUCTLIST', HOME_URL.'productlist');
define('P_CATEGORY', HOME_URL.'category');
__________________________________________________ ______
In classes/class.shopcore.php
find:
foreach ($aParams as $key => $val) {
if ( empty($val) ) {
continue;
}
if ($i == 0) {
$sReturn .= '?';
}
else {
$sReturn .= '&';
}
$sReturn .= $key.'='.$val;
$i++;
}
replace with:
foreach ($aParams as $key => $val) {
if ( empty($val) ) {
continue;
}
if ($i == 0) {
$sReturn .= '/';
}
else {
$sReturn .= '/';
}
$sReturn .= $key.'/'.$val;
$i++;
}
__________________________________________________ ___________
find:
'|'
replace with:
'~'
(5 of these)
__________________________________________________ ___________
find:
$sScriptName = preg_replace('/\/.*\//', '', $_SERVER['SCRIPT_NAME']);
$sScriptName = str_replace('/', '', $sScriptName ); // pop the slash if there's still one
$sScriptName = HOME_URL.$sScriptName;
replace with:
$sScriptName = preg_replace('/\/.*\//', '', $_SERVER['SCRIPT_NAME']);
$sScriptName = str_replace('/', '', $sScriptName ); // pop the slash if there's still one
$sScriptName = HOME_URL.$sScriptName;
$sScriptName = str_replace('.php','',$sScriptName);
__________________________________________________ ___________
In elements\refineby.php
find:
$sRefineByQuery = $oShopCore->createRefineByQuery($oShopCore->aRefineByActiveVals);
$sRefineByQuery .= !empty($sRefineByQuery) ? '|' : '';
replace with:
$sRefineByQuery = $oShopCore->createRefineByQuery($oShopCore->aRefineByActiveVals);
$sRefineByQuery .= !empty($sRefineByQuery) ? '~' : '';
and pop this in your .htaccess file (backup the original if you have one already)
RewriteEngine on
RewriteRule category/rb/(.*) category.php?rb=$1 [L]
RewriteRule product/p/(.*) product.php?p=$1 [L]
RewriteRule productlist/rb/(.*) productlist.php?rb=$1 [L]
RewriteRule productlist/q/(.*)/rb/(.*) productlist.php?q=$1&rb=$2 [L]
RewriteRule goto/rb/(.*) goto.php?rb=$1 [L]
RewriteRule goto/p/(.*)/rb/(.*) goto.php?p=$1&rb=$2 [L]
there are one or two issues, eg. the pagination on the productlist, still showing 'ugly' URL's
I'll post the code for those when I get round to it :rolleyes:
Hope this helps. :cool:
Anthony
I've just updated this to remedy a couple of issues, the pagination is still ugly, but the rest of it works much better now
Not tried this yet - but thanks for posting and I look forward to giving it a go!
Cheers
Bud
:)
Thank you very much for sharing this, it's been something that people have been very reluctant to share in the past for some reason...
I'll be implementing this very soon, it's one of those stupid area's that i've struggled with, so to date i've never bothered actually implementing friendly links, as I end up just confusing myself and going round in circles.
Again, a big thanks from me, I really appreciate it and i'm sure plenty of others will too.
currentstyle
26-02-09, 00:31
it's been something that people have been very reluctant to share in the past for some reason
Theres the old, sometimes misgiven notion of retaining an advantage in the affiliate world, but, at the risk of sounding like a socialist, we're all in this together?
There are one or two more parts to sort out, essentially the transformation of these url's
productlist.php?rb=4-189&iListOffset=10
into productlist/rb/4-189/offset/10 etc.
Thats for another day :)
Anyone implement this?
I can not make working the search function. I get 404 error for this search for example: http://www.mydomain.com/productlist?q=sony&rb=4-0
Anyone implement this?
I can not make working the search function. I get 404 error for this search for example: http://www.mydomain.com/productlist?q=sony&rb=4-0
Not yet, it's on my to do list for this week. If you've not had any other confirmation by that point though, i'll let you know how i've got on with it.
currentstyle
02-03-09, 20:10
Anyone implement this?
I can not make working the search function. I get 404 error for this search for example: http://www.mydomain.com/productlist?q=sony&rb=4-0
There isn't actually a rewrite rule to transform the url you cite in your example, thats not really the purpose of mod_rewrite, i did say there were issues, and you have brought one to light, thank you.
For this issue, in templates\default\elements\search_box.tpl (assuming you're using the default template)
find:
<form name="search" action="{$P_PRODUCTLIST}" method="GET" onsubmit="this.submit();return false;">
replace with
<form name="search" action="productlist.php" method="GET" onsubmit="this.submit();return false;">
Thanks again for finding this
ALSO
In templates\default\elements\nav_product_list_top.tp l
find:
<form class="" action="{$PRODUCTLIST_PAGE}" method="GET">
replace with:
<form class="" action="productlist.php" method="GET">
Anthony
..........it's been something that people have been very reluctant to share in the past for some reason...
Because it's hard graft mate. Once you start trying to get the pagination, breadcrumbs etc in line, preventing old style url execution, form jump scripts, security checks, 404 bad urls etc, it gets abit beyond what most people would want to share.
You can start with simple mods, but once G get's his filthy fingers into your nooks and crannies, you end up with all sorts of mess that takes alot of time to fix and prevent from reoccurring. It took most of V1's lifetime to fix to a decent, rankable standard and I still have dupe issues.
I think this is why that dude opted to go straight for wordpress.
Obviously, in the spirit of GNU gpl, we are obliged to share our scripts when asked to, but in the same spirit we are allowed to put a price on that sharing. Our experience falls outside the gpl, and comes for free, which is one of my main reasons for posting here and the beauty of a community forum.
I hope Anthony doesn't mind me chipping in (it is GNU afterall). If everyone stuck their oar in we could probably come up with a pretty decent mod between the community.
The .htaccess code above, I would suggest changing some of the regex for security reasons.
for instance:
RewriteRule category/rb/(.*) category.php?rb=$1 [L]should really be:
RewriteRule category/rb/4-([0-9]*) category.php?rb=4-$1 [L]otherwise domain. cam/ (http://domain.cam/)category/rb/Ill_put_anything_I_like_in_this_url_thanks will be processed when you really want it to throw a 404.
There isn't actually a rewrite rule to transform the url you cite in your example, thats not really the purpose of mod_rewrite, i did say there were issues, and you have brought one to light, thank you.
For this issue, in templates\default\elements\search_box.tpl (assuming you're using the default template)
find:
<form name="search" action="{$P_PRODUCTLIST}" method="GET" onsubmit="this.submit();return false;">
replace with
<form name="search" action="productlist.php" method="GET" onsubmit="this.submit();return false;">
Thanks again for finding this
ALSO
In templates\default\elements\nav_product_list_top.tp l
find:
<form class="" action="{$PRODUCTLIST_PAGE}" method="GET">
replace with:
<form class="" action="productlist.php" method="GET">
Anthony
Great!
Thanks for helping us.
currentstyle
03-03-09, 10:34
If everyone stuck their oar in we could probably come up with a pretty decent mod between the community.
I second that.
I'm working on the pagination as we speak, because, as you point out, duplicate pages are one of the things that search engines will penalise, and obviously;
productlist/rb/4-193~3-802
is the same page as;
productlist.php?rb=4-193~3-802&iListOffset=0
The breadcrumbs, sortby and products per page as well as the remove active filters sections are all done. And with your htaccess improvement, then it becomes a little more robust.
I hope Anthony doesn't mind me chipping in
Heaven forfend, the whole reason for posting here is so that we can all pick up the ball and run with it.:)
...........the whole reason for posting here is so that we can all pick up the ball and run with it.:)
Nicely! And a cool Rugby analogy
In which case, I'll pass on more..........
The iListOffset=0 was a large issue. I definitely didn't have any links to any pages with that url, but G likes to play around. Dont expect them to stick to your linking structure. If you have iListOffset=10, it's a fair bet they'll want to know what happens when they change the integer, which is where we need to add our robots.txt into the armoury as well as some happy php:
Disallow: *iListOffset=0
if($_GET['iListOffset'] < '10'){
$url = 'Page url without the iListOffset';
header("HTTP/1.1 301 Moved Permanently");#301
header("Location: ".$url."");
exit();
}
you could use htaccess for the 301, but I prefer php so I can make a log of pages.
Found another 404 Error return for Popular Searches like this
http://www.mysite.com/productlist/q/laptop
I think elements/query_list.php need some changes, maybe this line
$oQuery->sLink = $oShopCore->buildLink(P_PRODUCTLIST, '', '', $oQuery->iCategoryId, $oQuery->sQuery);
currentstyle
03-03-09, 13:23
replace the following line in .htaccess
RewriteRule productlist/q/(.*)/rb/(.*) productlist.php?q=$1&rb=$2 [L]
with
RewriteRule productlist/q/(.*)/rb/(.*) productlist.php?q=$1&rb=$2 [L]
RewriteRule productlist/q/(.*) productlist.php?q=$1 [L]
Thanks
I see another error on the error_log file. This error appears every time I perform a search
[03-Mar-2009 13:56:33] PHP Warning: Invalid argument supplied for foreach() in /home/public_html/mysite.com/classes/class.shopcore.php on line 406
the line 406 is
foreach ( $this->aPathCategories as $oCategory) {
Ok, finally got this implemented on one of my sites and it seems to be working very well, I have only found one error so far...
When on the productlist page, having a single active refine doesn't work properly. The active refine shows properly, but the link doesn't work, it leads to a 404 page instead. Having 2 active refines however works perfectly.
What i've done as a slight work around for now, is change the following code:
In refineby.tpl template:
{if count($aCancelRefineByLinks)>0 }
Changed to:
{if count($aCancelRefineByLinks)>1 }
Basically making it so that if there is only 1 active refine, it doesn't show at all.
It gets rid of the error anyway, although i'm sure there is a better more practical fix that could be done.
The difference in visits over the weekend on the site i've implemented this on has been massive by the way, I actually ran out of quota for the first times since using Shop Window, which in one way is fantastic so thanks for providing this stuff, I'm just hoping SW will bump my limit up a little as it could well become a regular occurence and could get a little embarassing.
Nicely! And a cool Rugby analogy
In which case, I'll pass on more..........
The iListOffset=0 was a large issue. I definitely didn't have any links to any pages with that url, but G likes to play around. Dont expect them to stick to your linking structure. If you have iListOffset=10, it's a fair bet they'll want to know what happens when they change the integer, which is where we need to add our robots.txt into the armoury as well as some happy php:
Disallow: *iListOffset=0
if($_GET['iListOffset'] < '10'){
$url = 'Page url without the iListOffset';
header("HTTP/1.1 301 Moved Permanently");#301
header("Location: ".$url."");
exit();
}
you could use htaccess for the 301, but I prefer php so I can make a log of pages.
Thanks for all the info got it working but with a few glitches, the listoffset code above where do I put it, what file? I've done the robots.txt disallow.