Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

301 redirect


Halfpint

Recommended Posts

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

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>

 

 

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

function permanant_redirect($to) {

  header("HTTP/1.1 301 Moved Permanently");

  header("Location: http://$to");

  exit();

}

 

 

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

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();

}

 

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

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.

 

 

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

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?

 

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

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.

 

 

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

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

 

 

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

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');

}

 

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

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');

}

 

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

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');

 

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

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'];

 

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

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)

 

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

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

 

hope you don't mind I sent you a pm

Link to comment
Share on other sites

hope you don't mind I sent you a pm

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

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']);

}

 

 

 

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

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?

 

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

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 ?

 

 

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

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

 

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

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

 

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

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

 

 

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

  • 4 weeks later...

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...