Halfpint Posted August 18, 2005 Share Posted August 18, 2005 I am unable to use htaccess to implement a 301 redirect and I wondered if there was any other way to achieve this, I think the code below is what I require, however I need an alternative to using the htacess. Options +FollowSymLinks <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^mydomain\.co.uk [NC] RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L] </IfModule> Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 I am unable to use htaccess to implement a 301 redirect and I wondered if there was any other way to achieve this, I think the code below is what I require, however I need an alternative to using the htacess. Options +FollowSymLinks <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^mydomain\.co.uk [NC] RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L] </IfModule> <{POST_SNAPBACK}> function permanant_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 function permanant_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } <{POST_SNAPBACK}> Thank you for the quick reply, can you please explain in which file I should place the code and exactly what the code should look like ie. header("Location: http://what goes in here$to"); exit(); } Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 Thank you for the quick reply, can you please explain in which file I should place the code and exactly what the code should look like ie. header("Location: http://what goes in here$to"); exit(); } <{POST_SNAPBACK}> well, I don't know what redirect you want from that apache code. the function takes the $to parameter so you can put it in the top of application_top.php and call it with : function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: $to"); exit(); } if (requested url is not what you wanted) { if ($request_type == 'NONSSL') { $to = HTTP_CATALOG_SERVER . DIR_WS_CATALOG; } elseif ($request_type == 'SSL') { if (ENABLE_SSL_CATALOG == 'true') { $to = HTTPS_CATALOG_SERVER . DIR_WS_CATALOG; } else { $to = HTTP_CATALOG_SERVER . DIR_WS_CATALOG; } permanent_redirect($to); } or if (requested url is not what you wanted) { $to = tep_href_link('what you wanted', tep_get_all_get_params(), $request_type); permanent_redirect($to); } something like those but it depends on the redirect you seek. Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 well, I don't know what redirect you want from that apache code. the function takes the $to parameter so you can put it in the top of application_top.php and call it with : function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: $to"); exit(); } if (requested url is not what you wanted) { if ($request_type == 'NONSSL') { $to = HTTP_CATALOG_SERVER . DIR_WS_CATALOG; } elseif ($request_type == 'SSL') { if (ENABLE_SSL_CATALOG == 'true') { $to = HTTPS_CATALOG_SERVER . DIR_WS_CATALOG; } else { $to = HTTP_CATALOG_SERVER . DIR_WS_CATALOG; } permanent_redirect($to); } or if (requested url is not what you wanted) { $to = tep_href_link('what you wanted', tep_get_all_get_params(), $request_type); permanent_redirect($to); } something like those but it depends on the redirect you seek. <{POST_SNAPBACK}> Hi thanks again for the quick reply what I wish to do is to redirect my http URL to my www URL using a 301 redirect, so if I understand correctly I need to put the code in the top of application_top.php so it would look like this? <?php /* $Id: application_top.php,v 1.280 2003/07/12 09:38:07 hpdl Exp $ osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright © 2003 osCommerce Released under the GNU General Public License */ function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: $to http://www.mydomain.co.uk/$"); exit(); } // start the timer for the page parse time log define('PAGE_PARSE_START_TIME', microtime()); // set the level of error reporting error_reporting(E_ALL & ~E_NOTICE); // check if register_globals is enabled. // since this is a temporary measure this message is hardcoded. The requirement will be removed before 2.2 is finalized. if (function_exists('ini_get')) { ini_get('register_globals') or exit('FATAL ERROR: register_globals is disabled in php.ini, please enable it!'); } // Set the local configuration parameters - mainly for developers if (file_exists('includes/local/configure.php')) include('includes/local/configure.php'); // include server parameters require('includes/configure.php'); etc etc etc Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 When I insert this code: <?php // Permanent redirection header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.mydowmin.co.uk/"); exit(); ?> into my application_top.php file I get this message: Redirection limit for this url exceeded any ideas why? Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 When I insert this code: <?php // Permanent redirection header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.mydowmin.co.uk/"); exit(); ?> into my application_top.php file I get this message: Redirection limit for this url exceeded any ideas why? <{POST_SNAPBACK}> probably because it loops from http://www.mydowmin.co.uk/ to http://www.mydowmin.co.uk/ you cannot simply redirect this way without an if statement unless you redirect to another domain. Unconditionally redirecting to yourself will loop. Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 probably because it loops from http://www.mydowmin.co.uk/ to http://www.mydowmin.co.uk/ you cannot simply redirect this way without an if statement unless you redirect to another domain. Unconditionally redirecting to yourself will loop. <{POST_SNAPBACK}> I'm sorry but I'm not very good with php Can you please tell me what the if statment should look like so that it someone types http://mydomain.co.uk they get redirected to http://www.mydomain.co.uk Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 I'm sorry but I'm not very good with php Can you please tell me what the if statment should look like so that it someone types http://mydomain.co.uk they get redirected to http://www.mydomain.co.uk <{POST_SNAPBACK}> try this : function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['SERVER_NAME'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk'); } Treasurer MFC Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 try this : function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['SERVER_NAME'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk'); } <{POST_SNAPBACK}> or maybe better try HTTP_HOST instead of SERVER_NAME as I suspect that is coming from your system and not the request. Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 try this : function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['SERVER_NAME'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk'); } <{POST_SNAPBACK}> I tried that and nothing happened myaybe I'm inserting the code in the wrong place in the application_top.php would you please show me where I should be inserting it here is the first part of my application_top.php <?php /* $Id: application_top.php,v 1.280 2003/07/12 09:38:07 hpdl Exp $ osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright © 2003 osCommerce Released under the GNU General Public License */ // start the timer for the page parse time log define('PAGE_PARSE_START_TIME', microtime()); // set the level of error reporting error_reporting(E_ALL & ~E_NOTICE); // check if register_globals is enabled. // since this is a temporary measure this message is hardcoded. The requirement will be removed before 2.2 is finalized. if (function_exists('ini_get')) { ini_get('register_globals') or exit('FATAL ERROR: register_globals is disabled in php.ini, please enable it!'); } // Set the local configuration parameters - mainly for developers if (file_exists('includes/local/configure.php')) include('includes/local/configure.php'); // include server parameters require('includes/configure.php'); Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 I tried that and nothing happened myaybe I'm inserting the code in the wrong place in the application_top.php would you please show me where I should be inserting it here is the first part of my application_top.php <?php /* $Id: application_top.php,v 1.280 2003/07/12 09:38:07 hpdl Exp $ osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright ? 2003 osCommerce Released under the GNU General Public License */ // start the timer for the page parse time log define('PAGE_PARSE_START_TIME', microtime()); // set the level of error reporting error_reporting(E_ALL & ~E_NOTICE); // check if register_globals is enabled. // since this is a temporary measure this message is hardcoded. The requirement will be removed before 2.2 is finalized. if (function_exists('ini_get')) { ini_get('register_globals') or exit('FATAL ERROR: register_globals is disabled in php.ini, please enable it!'); } // Set the local configuration parameters - mainly for developers if (file_exists('includes/local/configure.php')) include('includes/local/configure.php'); // include server parameters require('includes/configure.php'); <{POST_SNAPBACK}> well, I would suggest to change 'mydomain.co.uk' into what is really is. maybe a good idea to first echo out the values so you can see what they contain: echo $_SERVER['HTTP_HOST']; Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 well, I would suggest to change 'mydomain.co.uk' into what is really is. maybe a good idea to first echo out the values so you can see what they contain: echo $_SERVER['HTTP_HOST']; <{POST_SNAPBACK}> Thank You so much in my includes\application_top.php right after the first <?php I placed this code: function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk.'); } and it now works perfectly (obviously I put the correct domain name in but for the forums I have changed it to mydomain) Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 Thank You so much in my includes\application_top.php right after the first <?php I placed this code: function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk.'); } and it now works perfectly (obviously I put the correct domain name in but for the forums I have changed it to mydomain) <{POST_SNAPBACK}> that is good news, I knew it should work as I tested it on my site as well. Still, you might want to look at using the tep_href_link function with this as now if someone requests mydomain.co.uk/index.php?products_id=12 they will be redirected to www.mydomain.co.uk (without the parameters). So use the tep_href_link..... example I gave you earlier for the redirect link so that mydomain.co.uk/index.php?products_id=12 becomes www.mydomain.co.uk/index.php?products_id=12 Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 that is good news, I knew it should work as I tested it on my site as well. Still, you might want to look at using the tep_href_link function with this as now if someone requests mydomain.co.uk/index.php?products_id=12 they will be redirected to www.mydomain.co.uk (without the parameters). So use the tep_href_link..... example I gave you earlier for the redirect link so that mydomain.co.uk/index.php?products_id=12 becomes www.mydomain.co.uk/index.php?products_id=12 <{POST_SNAPBACK}> hope you don't mind I sent you a pm Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 hope you don't mind I sent you a pm <{POST_SNAPBACK}> well, my spoken english is very bad so let me explain in text. if someone, and you can do this yourself, requested a page from your site, either directly or via a link on google or yahoo that reads: mydomain.co.uk/product_info.php?products_id=12 then your site would read the HTTP_HOST value, see it is mydomain.co.uk and then redirect to : www.mydomain.co.uk. that means that instead of going to the product info page of product 12, they are getting your main page. so you need to make sure that not only is the host name changed but that also the page name and the parameters are used in the redirection. so you need, in this case, to redirect from : mydomain.co.uk/product_info.php?products_id=12 to www.mydomain.co.uk/product_info.php?products_id=12 and not form: mydomain.co.uk/product_info.php?products_id=12 to mydomain.co.uk/ so the simplest way would be to add the $_SERVER['REQUEST_URI'] value which holds everything that comes after the host. so try this one. if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk' . $_SERVER['REQUEST_URI']); } Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 well, my spoken english is very bad so let me explain in text. if someone, and you can do this yourself, requested a page from your site, either directly or via a link on google or yahoo that reads: mydomain.co.uk/product_info.php?products_id=12 then your site would read the HTTP_HOST value, see it is mydomain.co.uk and then redirect to : www.mydomain.co.uk. that means that instead of going to the product info page of product 12, they are getting your main page. so you need to make sure that not only is the host name changed but that also the page name and the parameters are used in the redirection. so you need, in this case, to redirect from : mydomain.co.uk/product_info.php?products_id=12 to www.mydomain.co.uk/product_info.php?products_id=12 and not form: mydomain.co.uk/product_info.php?products_id=12 to mydomain.co.uk/ so the simplest way would be to add the $_SERVER['REQUEST_URI'] value which holds everything that comes after the host. so try this one. if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk' . $_SERVER['REQUEST_URI']); } <{POST_SNAPBACK}> Once again thank you for your fast reply Would I be right in thinking that I need to insert this code: if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk' . $_SERVER['REQUEST_URI']); } right after this code: function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['HTTP_HOST'] == 'a12c4magic.co.uk') { permanent_redirect('www.a12c4magic.co.uk.'); } in my includes\application_top.php or does it need to go else where within that file? Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 Once again thank you for your fast reply Would I be right in thinking that I need to insert this code: if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk' . $_SERVER['REQUEST_URI']); } right after this code: function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['HTTP_HOST'] == 'a12c4magic.co.uk') { permanent_redirect('www.a12c4magic.co.uk.'); } in my includes\application_top.php or does it need to go else where within that file? <{POST_SNAPBACK}> no, you simply replace this code you are using now: if ($_SERVER['HTTP_HOST'] == 'a12c4magic.co.uk') { permanent_redirect('www.a12c4magic.co.uk.'); } with this code : if ($_SERVER['HTTP_HOST'] == 'a12c4magic.co.uk') { permanent_redirect('www.a12c4magic.co.uk' . $_SERVER['REQUEST_URI']); } so that ALL pages regardless of filenames and parameters are redirected with that info to the www host. PS. why did you put the last . after uk ? Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 no, you simply replace this code you are using now: if ($_SERVER['HTTP_HOST'] == 'a12c4magic.co.uk') { permanent_redirect('www.a12c4magic.co.uk.'); } with this code : if ($_SERVER['HTTP_HOST'] == 'a12c4magic.co.uk') { permanent_redirect('www.a12c4magic.co.uk' . $_SERVER['REQUEST_URI']); } so that ALL pages regardless of filenames and parameters are redirected with that info to the www host. PS. why did you put the last . after uk ? <{POST_SNAPBACK}> When I replaced the code with the one you suggest I received this error Fatal error: Call to undefined function: permanent_redirect() in /home/a/c/acmagic/public_html/includes/application_top.php on line 3 The extra . after the uk was a mistake although it still worked go figure Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 When I replaced the code with the one you suggest I received this errorFatal error: Call to undefined function: permanent_redirect() in /home/a/c/acmagic/public_html/includes/application_top.php on line 3 The extra . after the uk was a mistake although it still worked go figure <{POST_SNAPBACK}> ok, well, do not remove the function definition, the entire code should read: function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['HTTP_HOST'] == 'a12c4magic.co.uk') { permanent_redirect('www.a12c4magic.co.uk' . $_SERVER['REQUEST_URI']); } Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 When I replaced the code with the one you suggest I received this errorFatal error: Call to undefined function: permanent_redirect() in /home/a/c/acmagic/public_html/includes/application_top.php on line 3 The extra . after the uk was a mistake although it still worked go figure <{POST_SNAPBACK}> oops that was my mistake I now have it working here is the code I have (obviously I put the correct domain name in but for the forums I have changed it to mydomain wish I had remember to that previousl because now I can expect yet more spam) <?php function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk' . $_SERVER['REQUEST_URI']); } Thank you so much for all your help, I hope it helps others too Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 oops that was my mistake I now have it working here is the code I have (obviously I put the correct domain name in but for the forums I have changed it to mydomain wish I had remember to that previousl because now I can expect yet more spam) <?php function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['HTTP_HOST'] == 'mydomain.co.uk') { permanent_redirect('www.mydomain.co.uk' . $_SERVER['REQUEST_URI']); } Thank you so much for all your help, I hope it helps others too <{POST_SNAPBACK}> that is ok, it should work now for all your pages. Just ask the moderators to delete the posts with your real address in them, I am sure they will do that for you. Treasurer MFC Link to comment Share on other sites More sharing options...
boxtel Posted August 18, 2005 Share Posted August 18, 2005 that is ok, it should work now for all your pages. Just ask the moderators to delete the posts with your real address in them, I am sure they will do that for you. <{POST_SNAPBACK}> if they are around when you need them ofcourse. Treasurer MFC Link to comment Share on other sites More sharing options...
Halfpint Posted August 18, 2005 Author Share Posted August 18, 2005 if they are around when you need them ofcourse. <{POST_SNAPBACK}> ok I sent an email although not sure if I sent it to the correct person *fingers crossed* Once again I wish to thank you for all your help, I'm sure others will find this information useful too. :) Link to comment Share on other sites More sharing options...
DAVID3733 Posted September 10, 2005 Share Posted September 10, 2005 I been trying to divert pages still on search engines to new ones, can i use the method you have discused here, I am on a IIs server, this is what i have done so far <?php function permanent_redirect($to) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$to"); exit(); } if ($_SERVER['HTTP_HOST'] == 'www.mydomain.com/product_info.php?pName=bosch-gsb-12vsp2-gbm-12ves2-gli-12v-asg-52-ahs-a-accu-grs-12v-ve2-vet-ves2-ves3-vsh2-vpe2-psb-12vsp2-psr-12ves2-2-607-335-055-151-081-244-250-battery') { permanent_redirect('www.mydomain.com/product_info.php?pName=bosch-skil-12v-1900mah-battery-bat011&cName=power-tool-batteries-bosch' . $_SERVER['REQUEST_URI']); } This I have placed in the includes/application_top.php file Unfortuantly nothing is happening, link still goes to product not found any clues? David David Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.