301 Redirect PHP – How to redirect a PHP page or website


So you have an old php page you want to redirect to a new php page on your website and you don’t want to lose your search engine rankings. You also don’t want to lose the link juice because many sites are linking to it. The best way to accomplish this is to place 301 redirect code in your .htaccess file then upload it to the root of your server hosting the old site. When someone enters the old URL in their browser it will automatically redirect to the new page.

5 ways to redirect PHP web pages or websites

1. To redirect an old PHP page to a new PHP page

Place this 301 redirect code in the .htaccess file of the old site

redirect 301 /oldpage.php   http://domain.com/newpage.php

Replace oldpage.php with the page you wish to redirect. Replace newpage.php with the name of your new page. Write the code all on one line. If you have multiple pages that you want to redirect place the lines under each other.

2. To redirect an old PHP website to a new PHP site

Place this 301 redirect code in the .htaccess file of the site you want to redirect (not the new site)

redirect 301 / http://www.example.com/

Replace example.com with your own domain then upload the .htaccess file to the root of the server hosting your old domain. Test the 301 redirect works by entering the URL of your old domain in your browser. It will automatically redirect to the new domain.

3. To redirect .htm pages to .php pages

Check with your web host you have Mod_Rewrite installed (most Apache servers do). Place this code in the .htaccess file of your website.


RewriteEngine on
RewriteBase /
RewriteRule (.*).htm$ /$1.php

If you want to redirect a .html page to a .php page replace .htm with .html. Test the redirect works correctly by entering the old URL in your browser. It should redirect to the new .php page.

Alternatively you may use this code to redirect .html pages to .php pages.

RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php


Example

http://www.domain.com/yourpage.html will redirect to http://www.domain.com/yourpage.php

4. To redirect domain.com/index.php to http://www.domain.com/

Search engines will give you a duplicate content penalty if your home page can be accessed using both http://www.domain.com/ and domain.com/index.php. It sees two different pages. To prevent this from happening place this code in the .htaccess file of your website and upload it to your web server.


Options +FollowSymLinks
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

Make sure the 4th line ends with HTTP/
This code will also redirect domain.com/folder/index.php to domain.com/folder/.

5. 301 redirect via PHP

If you permanently want to redirect a page via PHP (without using .htaccess), place this code before all the content on your web page(ie.g. at the very top of the document):


<?php // Permanent 301 Redirect via PHP
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.domain.com/new/location/");
exit();
?>

Alternately, this code can be shortened to a single line as follows:


<?php header("Location: http://www.domain.com/new/location/", true, 301); ?>

Write all the above code on one line. Replace http://www.domain.com/new/location/ with the URL of the target page you wish to redirect to.

Tips and Tricks

* Note that using the 301 PHP redirect code and .htaccess file only works for a website running on an Apache Web Server.

* Make sure you test the web page or website redirection works correctly. It’s easy to leave out a piece of code and generate errors.

* Create a 404 error page so visitors will get redirected to this page (and remain on your website) in case you created an incorrect .htaccess file.

Related Articles
How to Redirect a Website or Web Page and Preserve Your Rankings
301 Redirect for Multiple Domains
How to Redirect a Web Page Using a 301 Redirect
301 vs 302 Redirect

**********************
Need help with the Design, Redesign or SEO of your website?
Go to: Website Design Maryland

Comments

  1. I haven’t checked in here for some time as I thought it was getting boring, but the last several posts are good quality so I guess I’ll add you back to my everyday bloglist. You deserve it friend 🙂

  2. Rebekah Detone says

    Thanks for the great info on analyzing competitor backlinks. Also, i’m gonna check out that Social Monkee’ service you show’d on the page.

  3. If you have a WordPress website install the WPTouch plugin

  4. Hello! Your site is loading lagging in my opinion, this kind
    of went on like a minute or two to actually load, I actually
    have no idea whether it is just simply me or your blog although facebook worked for me.
    Nevertheless, Thanks for creating an extraordinarily impressive
    article. Most people who actually discovered this site should have discovered this short article
    absolutely very helpful. This one is undoubtedly fantastic what you have implemented and want to discover more great content from your website.
    Immediately after checking out the post, I’ve book marked your web page.

  5. Superb blog! Do you have any helpful hints for aspiring writers?
    I’m planning to start my own site soon but I’m
    a little lost on everything. Would you recommend starting with a free platform like WordPress or
    go for a paid option? There are so many choices out there that I’m totally confused ..
    Any recommendations? Thanks a lot!

Trackbacks

  1. […] This post was mentioned on Twitter by Herman Drost, Herman Drost. Herman Drost said: How to redirect a PHP page or website http://www.drostdesigns.com/301-redirect-php-how-to-redirect-a-php-page-or-website/ […]

Speak Your Mind

*