gevork Posted December 23, 2009 Share Posted December 23, 2009 Hi Everyone, Firstly Happy Holidays! :D I was hoping to move some old pages to new ones after a category change using .htaccess here is what i coded; redirect 301 /blue-jeans-p-17.html http://www.domain.com/blue-jeans-different-p-22.html? but this just forwards to this; product_info.php because above in my htaccess I have another rule that is complicating things... RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING} (I need this rule for my ulitmate seo install) My question is how do I override or sort out this problem so that the old URL's with -p- in them can be forwarded to whatever new urls I want without complications (or going to product_info.php). Many Thanks in advance!! Gevork Link to comment Share on other sites More sharing options...
MrPhil Posted December 23, 2009 Share Posted December 23, 2009 Have you tried moving your new redirection entry ahead of the old one? It sounds like the old one (pattern: ^(.*)-p-(.*).html$) is being processed before the new one (pattern: /blue-jeans-p-17.html). Link to comment Share on other sites More sharing options...
♥FWR Media Posted December 23, 2009 Share Posted December 23, 2009 Hi Everyone, Firstly Happy Holidays! :D I was hoping to move some old pages to new ones after a category change using .htaccess here is what i coded; redirect 301 /blue-jeans-p-17.html http://www.domain.com/blue-jeans-different-p-22.html? but this just forwards to this; product_info.php because above in my htaccess I have another rule that is complicating things... RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING} (I need this rule for my ulitmate seo install) My question is how do I override or sort out this problem so that the old URL's with -p- in them can be forwarded to whatever new urls I want without complications (or going to product_info.php). Many Thanks in advance!! Gevork I think people use .htaccess and RewriteRules too much it is resource intensive and dangerous if wrong. It is simply done with a small PHP script .. 1) Create a new file in catalog/includes/ called redirects.php containing .. <?php $redirects = array(); /** * Redirect Seo Uris * * The first uri is the target (to be redirected), the second is the actual redirect. */ $redirects['my-old-product-p-2439.html'] = 'my-new-product-p-3938.html'; $redirects['my-old-category-c-3937.html'] = 'my-new-category-c-4485.html'; ?> 2) Create a new file in catalog/includes/classes/ called redirects_301.php containing .. <?php class redirects301 { var $redirects = array(); function redirects301(){ if ( is_readable( DIR_WS_INCLUDES . 'redirects.php' ) ){ include_once DIR_WS_INCLUDES . 'redirects.php'; $this->redirects = $redirects; } $this->doRedirects(); } function doRedirects(){ if ( !empty($this->redirects) ){ foreach ( $this->redirects as $target => $redirect ){ if ( false !== strpos( $_SERVER['REQUEST_URI'], $target ) ){ $this->redirect( $redirect ); } } } } function redirect( $redirect ){ $uri = HTTP_SERVER . DIR_WS_CATALOG . $redirect; session_write_close(); header( "HTTP/1.0 301 Moved Permanently" ); header( 'Location: ' . $uri ); exit; } } ?> 3) catalog/includes/application_top.php Find ... require(DIR_WS_FUNCTIONS . 'html_output.php'); Add immediately BELOW .. // FWR Media 301 redirects include_once DIR_WS_CLASSES . 'redirects_301.php'; $redirects = new redirects301(); Now open up catalog/includes/redirects.php and make sure you understand how to create the keys and values of the array in order to create future redirections. Delete the dummy array and put in its place your own. Done .. You can now create product redirects with valid 301 redirection whenever you need to. Ultimate SEO Urls 5 PRO - Multi Language Modern, Powerful SEO Urls KissMT Dynamic SEO Meta & Canonical Header Tags KissER Error Handling and Debugging KissIT Image Thumbnailer Security Pro - Querystring protection against hackers ( a KISS contribution ) If you found my post useful please click the "Like This" button to the right. Please only PM me for paid work. Link to comment Share on other sites More sharing options...
♥FWR Media Posted December 23, 2009 Share Posted December 23, 2009 A cleaner way of writing the redirects.php array is as follows: - $redirects = array( 'my-old-product-p-2439.html'] = 'my-new-product-p-3938.html', 'my-old-category-c-3937.html'] = 'my-new-category-c-4485.html', 'my-old-category-c-57.html'] = 'my-new-category-c-1252.html' ); But the one in the script, I believe, the average user would find easier to understand. Ultimate SEO Urls 5 PRO - Multi Language Modern, Powerful SEO Urls KissMT Dynamic SEO Meta & Canonical Header Tags KissER Error Handling and Debugging KissIT Image Thumbnailer Security Pro - Querystring protection against hackers ( a KISS contribution ) If you found my post useful please click the "Like This" button to the right. Please only PM me for paid work. Link to comment Share on other sites More sharing options...
gevork Posted December 24, 2009 Author Share Posted December 24, 2009 I think people use .htaccess and RewriteRules too much it is resource intensive and dangerous if wrong. It is simply done with a small PHP script .. 1) Create a new file in catalog/includes/ called redirects.php containing .. <?php $redirects = array(); /** * Redirect Seo Uris * * The first uri is the target (to be redirected), the second is the actual redirect. */ $redirects['my-old-product-p-2439.html'] = 'my-new-product-p-3938.html'; $redirects['my-old-category-c-3937.html'] = 'my-new-category-c-4485.html'; ?> 2) Create a new file in catalog/includes/classes/ called redirects_301.php containing .. <?php class redirects301 { var $redirects = array(); function redirects301(){ if ( is_readable( DIR_WS_INCLUDES . 'redirects.php' ) ){ include_once DIR_WS_INCLUDES . 'redirects.php'; $this->redirects = $redirects; } $this->doRedirects(); } function doRedirects(){ if ( !empty($this->redirects) ){ foreach ( $this->redirects as $target => $redirect ){ if ( false !== strpos( $_SERVER['REQUEST_URI'], $target ) ){ $this->redirect( $redirect ); } } } } function redirect( $redirect ){ $uri = HTTP_SERVER . DIR_WS_CATALOG . $redirect; session_write_close(); header( "HTTP/1.0 301 Moved Permanently" ); header( 'Location: ' . $uri ); exit; } } ?> 3) catalog/includes/application_top.php Find ... require(DIR_WS_FUNCTIONS . 'html_output.php'); Add immediately BELOW .. // FWR Media 301 redirects include_once DIR_WS_CLASSES . 'redirects_301.php'; $redirects = new redirects301(); Now open up catalog/includes/redirects.php and make sure you understand how to create the keys and values of the array in order to create future redirections. Delete the dummy array and put in its place your own. Done .. You can now create product redirects with valid 301 redirection whenever you need to. Hi Guys, Thank's for you and Phil's responses. Firstly Phil the order doesnt seem to have any effect on how its processed in the .htaccess file. I have created your script and everything worked perfectly until firefox said this; This web page has a redirect loop. The web page at http://www.domain.com/changed-t-shirt-url-p-101.html has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. explorer stays on an constantly loading white page. a url with a product id that doesn't exist works and redirects like a dream! Turning off the product in admin doesnt work, changing the products id doesnt work. Many thanks in advance, G Link to comment Share on other sites More sharing options...
♥FWR Media Posted December 24, 2009 Share Posted December 24, 2009 Hi Guys, Thank's for you and Phil's responses. Firstly Phil the order doesnt seem to have any effect on how its processed in the .htaccess file. I have created your script and everything worked perfectly until firefox said this; This web page has a redirect loop. The web page at http://www.domain.com/changed-t-shirt-url-p-101.html has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. explorer stays on an constantly loading white page. a url with a product id that doesn't exist works and redirects like a dream! Turning off the product in admin doesnt work, changing the products id doesnt work. Many thanks in advance, G No reason at all for this to create a loop The first item in the array (key) should be the target that you wish to be 301 redirected. The second item in the array (value) should be the redirected destination uri. So for example if you had an old product that no longer exists the url of which was .. http://www.mysite.com/catalog/my-old-product-p-23.html And the the new product to be redirected to being .. http://www.mysite.com/catalog/my-new-product-p-85.html the array would be .. $redirects['my-old-product-p-23.html'] = 'my-new-product-p-85.html'; I can't see any reason for a redirect loop in there (unless you have retained the .htaccess redirect) Ultimate SEO Urls 5 PRO - Multi Language Modern, Powerful SEO Urls KissMT Dynamic SEO Meta & Canonical Header Tags KissER Error Handling and Debugging KissIT Image Thumbnailer Security Pro - Querystring protection against hackers ( a KISS contribution ) If you found my post useful please click the "Like This" button to the right. Please only PM me for paid work. Link to comment Share on other sites More sharing options...
tigergirl Posted April 14, 2010 Share Posted April 14, 2010 Hi, I Just wondered how we would redirect these on a 301 in a similar way: http://www.mysite.co...ex.php?cPath=30 http://www.mysite.co...php?cPath=30_32 http://www.mysite.co...products_id=121 I have a few old categories and products I want to get google off and I wish to delete another category and move things around so don't want to be getting 200s when they should be 301s. I didn't want to go to the bother of adding this mod if it will only work on SEO uris.... will this work? $redirects = array( 'index.php?cPath=30'] = 'index.php?cPath=32', 'product_info.php?products_id=121'] = 'product_info.php?products_id=324' ); Thanks I'm feeling lucky today......maybe someone will answer my post! I do try and answer a simple post when I can just to give something back. ------------------------------------------------ PM me? - I'm not for hire Link to comment Share on other sites More sharing options...
raffip Posted June 17, 2010 Share Posted June 17, 2010 I'm trying to make this work as it seems the cleverest way to deal with the 301s in OsC, however i get this: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at... etc Any ideas? Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.