digitalFAQ.com Forum

digitalFAQ.com Forum (https://www.digitalfaq.com/forum/)
-   Website and Server Troubleshooting (https://www.digitalfaq.com/forum/web-tech/)
-   -   How to write a PHP 301 redirect (https://www.digitalfaq.com/forum/web-tech/2952-how-write-php.html)

kpmedia 03-14-2011 10:55 PM

How to write a PHP 301 redirect
 
Quick tip...

Code:

<?php
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.somewhere.com/somepage.html');
?>

This is the full file -- no extra code (HTML, BODY, etc).

Save it as index.php if you're redirecting a folder or site.
Or some other PHP file that you're using as a redirect, i.e. theoldpage.php

Note: You don't have to redirect to a file -- it can be a folder or domain, too.
Examples:
Code:

header('Location: http://www.somewhere.com/');
header('Location: http://www.somewhere.com/folder/');

Trivia: You can also run HTML as PHP, to recycle an old HTML name and not rely on META redirects.
When the server is set to run HTML as PHP, simply save the code into a new HTML file -- i.e, index.html or index.htm

The better solution is to use .htaccess when available. :thumb:


All times are GMT -5. The time now is 07:44 AM

Site design, images and content © 2002-2024 The Digital FAQ, www.digitalFAQ.com
Forum Software by vBulletin · Copyright © 2024 Jelsoft Enterprises Ltd.