New Bookstore! Save 20% on books with discount code: LAUNCH
Web Dev + WordPress + Security

1-Minute Tutorial: Permanent (301) Redirect via PHP or htaccess

Here is an example of one of the most frequently asked PHP/htaccess-related questions I receive here at Perishable Press:

How do I redirect a specific page/URL using PHP/htaccess?

So common is this inquiry that I have decided to just post a quick, “one-minute” tutorial describing the technique.

Permanent (301) Redirect via PHP

To permanently redirect a page via PHP, place this code before all other content (i.e., at the very top of the document):

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

Alternately, this snippet may be consolidated into a single line as follows:

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

Once in place, this code (either method) will redirect the visitor to the address specified in the third line. Edit that address to match the redirect target and you’re all set. Note: when placed in a header file that is included in multiple web pages, this code will redirect all of them to the specified address.

Permanent (301) Redirect via htaccess

This one is even easier. To permanently redirect a page via htaccess, place this code in your target (e.g., root) htaccess file:

redirect 301 /path/to/old-file.php   http://domain.tld/path/to/new-file.php

This is a very common method of permanently redirecting single pages. Once in place, this code will redirect the visitor from the location listed first to the location listed second. Note that the first location is specified relative to the root directory (i.e., the directory in which the htaccess file is found), whereas the second location is specified as the complete URL.

Jeff Starr
About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
.htaccess made easy: Improve site performance and security.

6 responses to “1-Minute Tutorial: Permanent (301) Redirect via PHP or htaccess”

  1. Rick Beckman 2008/01/23 2:21 pm

    The two-liner call to PHP’s header() function can be simplified down to one line:

    header('Location: 'http://www.example.com/', true, 301);

    The second parameter (true) determines whether or not that header should replace similar headers which have already been specified; it requires PHP 4.0.4 or newer.

    The third parameter (301) is the HTTP status code that should be sent; it requires PHP 4.3.0 or newer.

    Both the 2nd & 3rd parameters are, of course, optional, but that 3rd parameter is a lot easier to use than crafting extra header() statements to send the proper HTTP response.

  2. Jeff Starr

    Excellent information, as usual, Rick.. I am most fortunate to have you as a reader :) Once again, thank you for keeping me in check! (Now, if I could only make up my mind regarding those dated permalinks..)

  3. Rick Beckman 2008/01/24 1:34 am

    I’ve benefited from what you’ve written here for a long time, and I’ve a long way to go in returning that favor! :D

  4. Jeff Starr
    Perishable 2008/01/27 9:35 am

    You are too kind! ;)

  5. Hi there.
    I’m trying to redirect all IPs except mine. I have the following in my root .htaccess file and all works great except it only redirects the home page. Anyone can still go to other parts of the site. I would really appreciate your thoughts.

    Note: RewriteEngine on is at start of file

    Options +FollowSymLinks
    # redirect all except developer ip addresses to alternate domain
    # home ip address
    RewriteCond %{REMOTE_ADDR} !^XXX.XXX.XX.XX$
    # work ip address
    # RewriteCond %{REMOTE_ADDR} !^XXX.XXX.XX\.XX$
    RewriteRule .* http://www.google.com/ [R=302,L]

  6. Jeff Starr
    Perishable 2008/06/08 8:00 am

    Hi Dean,
    Here is the code that works for me:

    RewriteCond %{REMOTE_ADDR} !^987\.654\.321\.987$
    RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.123$
    RewriteRule ^(.*)$ http://www.google.com/ [R=302,L]

    ..of course, you will want to change the IP addresses to match those of your own.
    — Good luck!

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
USP Pro: Unlimited front-end forms for user-submitted posts and more.
Thoughts
LOL @ “weekly” support: “Your request has been received and will be reviewed soon. Neil (your support guy) checks support requests weekly. Rest assured we’ll get to it!” — Gotta be kidding with this.
After several days of work, finally taking a break. If anyone needs me, I'll be in Zora’s Domain, working thru Mah Eliya shrine.
Never ever call the client a useless idiot. Even if it’s true.
Inevitable that Google shuts down Feedburner. Maybe this year? Who knows. Here is a nice summary plus alternatives.
There is no way that Marvel can serve up anything better than what they've already done with Infinity saga/Endgame. Knowhere to go but down, sadly.
I built a new search launchpad to make my life easier. So I can search multiple engines quickly and easily.
Updating all muh plugins for WP 5.5 imminent release! Wut August :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.