Page 1 of 1

Php link code

Posted: Thu Oct 06, 2005 11:17 am
by fredski
Is ther any code I can put into my php pages that will create a link as follows:-

The domain the link will be on is domain1.com

and for each page within that domain I need it to produce a link for the same page but on a different domain.

for example, say I had a page http://www.domain1.com/foobar.php

I need a link at the bottom of the page that links to http://www.domain2/foobar.php with the text 'old page'

As my site has thousands of pages genrated from a template I can't create a manual link for each one so I need some code that will generate it.

Any help would be fantastic and much appreciated.

Thanks

Posted: Thu Oct 06, 2005 11:46 am
by feyd
look into using parse_url()

Posted: Thu Oct 06, 2005 11:58 am
by fredski
Thanks for the reply Feyd,

But that page went right over my head, Im a bit of a noob when it comes to php and only just getting past cutting and pasting and getting my hands dirty.

Could you please expand on it a little?

Cheers

Posted: Thu Oct 06, 2005 12:35 pm
by John Cartwright
Example 1. parse_url() example

Code: Select all

$ php -r 'print_r(parse_url("http://username:password@hostname/path?arg=value#anchor"));'
Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

$ php -r 'print_r(parse_url("http://invalid_host..name/"));'
Array
(
    [scheme] => http
    [host] => invalid_host..name
    [path] => /
)

Posted: Fri Oct 07, 2005 3:56 am
by fredski
A little too complex for my needs.

Code: Select all

<a href="<?php echo "http://www.domain2.com".$_SERVER['PHP_SELF'] ?>">Old Page
</a>
Worked for my needs.

:)

Posted: Fri Oct 07, 2005 4:47 am
by fredski
Drat
Found a flaw in it.

Although that code works on a lot of my pages. On my store section my pages use mod rewrite so it takes a http://www.domain1.com/product_info.php?products_id=1 page and rewrites it to http://www.domain1.com/product-name.html but the code generated on the page for my link using the above code just states the http://www.domain2.com/product_info.php which isn't what I need.

Anyone know a work around for this?

Posted: Fri Oct 07, 2005 3:34 pm
by yakasha
$_SERVER['QUERY_STRING']

Posted: Mon Oct 10, 2005 7:59 am
by fredski
Thanks for the help so far. :)

Still not quite right though... :(

If I use the following code:-

Code: Select all

<a href=" <?php echo "http://www.domain2.com".$_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING'] ?>" class="emailNav1">Old page</a>
It returns the following link http://www.domain2/catalog/index.phpcPath=1_31

Thats wrong on two accounts

1) there should be a ? between php and cpath like index.php?cPath=1_31

2) Thats the un altered link before modrewrite changes catalog/index.php?cPath=1_31 to /catalog/category_name.html so the link that its generating to the old page is one that the search engines would of had indexed anyway so is irrelevent.

Anyone know of a fix? I need it to link to exactly whats shown in the adress bar with the only difference being the domain name.

Posted: Mon Oct 10, 2005 8:02 am
by Nathaniel
$_SERVER['REQUEST_URI'] might help you out.

- Nathaniel

Posted: Mon Oct 10, 2005 8:11 am
by fredski
Cheers Nathaniel,

That did the trick just fine :)

The following code gives me exactly whats displayed in the address bar with a different domain name

Code: Select all

<a href=" <?php echo "http://www.domain2.com".$_SERVER['REQUEST_URI'] ?>" class="emailNav1">Old page</a>
Magic, now when the search engine comes to my new pages it will see the link to the old page, follow it and find a 301 back to the page on new domain. That way it will know to take out the old pages from its index and replace with the new ones much better than if I just had a 301 on the old domain without any links on the new.