0

I have changed my blog url. (I know .htaccess can do this)

RewriteEngine on
RewriteRule (.*) http://my-new-website.com/$1 [R=301]

but I can't use .htaccess now (personal reasons.. I can use only PHP now)

I want to use a PHP code to do that.

I want to redirect

http://my-old-website.com/v/test/new.html

To

http://my-new-website.com/v/test/new.html

I have searched a lot online, and looked for more similar questions on stackoverflow, but I didn't find any question like this

3 Answers3

1

Just use a Location header.

header('Location: http://my-new-website.com' . $_SERVER['REQUEST_URI']);
header('Content-Type: text/html');
die('I have moved to my-new-website.com'); // for ancient browsers
jornane
  • 1,397
  • 10
  • 27
0

PHP has the header function to set custom headers. All you have to do is set the 'refresh' to a different page.

header('Refresh:10;url=newwebsite.php' . $_SERVER['REQUEST_URI']);

Page redirect after certain time PHP

Community
  • 1
  • 1
Nick Tucci
  • 336
  • 4
  • 16
  • i will the php file in my old website (my-old-website.com) and i want to redirect it to my new website... if the url is `a.com/post-one.html` it should redirect to `b.com/post-one.html` ONLY DOMIAN NAME SHOULD BE CHANGED (a,b are old and new sites respectively..) – user3888863 Jul 29 '14 at 18:29
  • I don't understand your problem. Just change 'newwebsite.php' to 'b.com/post-one.html'. – Nick Tucci Jul 29 '14 at 18:30
  • not only that URL... if it is `a.com/post-two.html` them it should redirect to `b.com/post-two.html` like that there are many pages :( .... got me ? :( – user3888863 Jul 29 '14 at 18:32
  • 1
    i think it won't change the domain name to `a.com` to `b.com` :/ :( – user3888863 Jul 29 '14 at 18:45
-1

you can do it with setting the Location header. The following will add the given $_GET parameters to and append them to the URL too:

$newUrl = "http://my-new-website.com/";
$currentPage = $_SERVER['PHP_SELF'];
//building the querystrging (if some GET variables where set):
$queryString = "";
if(count($_GET)) > 0) {
    $queryString = "?" . http_build_query($_GET);
} 

//set the header to redirect:
header('Location:'.$newUrl.$currentPage.$queryString);

ie: redirects http://example.com/v/file1.php?key1=val1&key2=val2 to http://my-new-website.com/v/file1.php?key1=val1&key2=val2

chresse
  • 5,486
  • 3
  • 30
  • 47
  • and the same code will work for `my-old-website.com.com/post-one.php` TO `my-new-website.com/post-one.php` right ? (the imporatnt thing is it should just change the domian name in url and keep the whole URL as same... and redirect..) – user3888863 Jul 29 '14 at 18:56
  • What kind of header is that? – PeeHaa Jul 29 '14 at 19:03
  • What does `$firstRun` do? – PeeHaa Jul 29 '14 at 19:04
  • @PeeHaa: `$firstRun` ensures that the first GET parameter will apended by `?` and the others with `&` – chresse Jul 29 '14 at 19:19
  • Yeah that was my first guess however your code tells me something different. – PeeHaa Jul 29 '14 at 19:23
  • @PeeHaa: sry. i forget to change it. was that your problem? – chresse Jul 29 '14 at 19:24
  • If there is no query string in the original URL OP now has a free `?` – PeeHaa Jul 29 '14 at 19:28
  • 1
    Also all that work when PHP offers http://php.net/manual/en/function.http-build-query.php – PeeHaa Jul 29 '14 at 19:29
  • 1
    @PeeHaa: thanks for the hint with the `http_build_query` function. i didn't know that. the free `?` at the end if no parameters were set shouldn't be a big problem. but i changed it... – chresse Jul 29 '14 at 19:35