Hi friend pls suggest me how to set 301 redirection in PHP. I have tried searching from internrt net and puted this code but this isnt helpd <?php if(!stristr($_SERVER["HTTP_HOST"], 'www')){ header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.otssolutions.com/" . $_SERVER["REQUEST_URI"]); exit(); } ?> Please tell me what to do and do ineed to se redirection on all pages or setting it for home page will do for everypage?
What redirect you want, I think you want with or without www redirect. If so then use below code:- Add the following four lines to your .htaccess file, replacing 'domain.com' with your site's domain. Remember, you need to be hosted on a Linux server with the Apache Mod-rewrite module enabled. Options +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^domain.com [nc] rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Can you just be a bit clear with your code. I mean an example. (Or provide an example) I too need to know about this redirect code.
Suppose your domain name is abc.com, you want when any one can try to open just abc.com then it will automatically redirect to www.abc.com. For implementing this redirect you have to create .htaccess file. File include following code:- Options +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^abc.com [nc] rewriterule ^(.*)$ http://www.abc.com/$1 [r=301,nc] After creating the file upload this file on root directory of your FTP. Make sure your apacha server rewrite module is enable.
PHP Single Page Redirect In order to redirect a static page to a new address simply enter the code below inside the index.php file. <?php header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.newdomain.com/page.html"); exit(); ?> PHP Canonical Redirect The Canonical 301 Redirect will add (or remove) the www. prefixes to all the pages inside your domain. The code below redirects the visitors of the http://domain.com version to http://www.domain.com. <?php if (substr($_SERVER['HTTP_HOST'],0,3) != ‘www’) { header(’HTTP/1.1 301 Moved Permanently’); header(’Location: http://www.’.$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']); } ?>
thanks for your valuable suggestion, where to put code in server for canonical redirection? do ineed to make any changes in this code or put as it is? pls pls reply
301 in php <? header( "HTTP/1.1 301 Moved Permanently" ); header( "Status: 301 Moved Permanently" ); header( "Location: http://www.new-url.com/" ); exit(0); // This is Optional but suggested, to avoid any accidental output ?>
You would need to put the php code in each php file called. I would suggest at the top of the page. However, my personal suggestion would be to do it with the .htaccess file as explained by The Rock in a previous reply. This is because you only need to put the code in once in the .htaccess file, and not copy it into each individual php file called. It also means you don't need to worry about remembering to add the code in to new php files you create. Overall a much neater way to manage your redirects IMO.
<?php if (substr($_SERVER['HTTP_HOST'],0,3) != ‘www’) { header(’HTTP/1.1 301 Moved Permanently’); header(’Location: http://www.’.$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']); } ?> where to put this code? .htaceess file?
no, that code is php so would need to go at the top of your php files if you choose to use php to perform the redirects. If you want to use .htaccess to perform the redirects, use the following code in your .htaccess file: Options +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^domain.com [nc] rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] Code (markup): PHP and .htaccess are 2 DIFFERENT methods for achieving the same result. Use 1 OR the other, not both.
I would HIGHLY suggest that you NOT implement 301 redirects for non-www-->www (or visa versa) in your code. If you do it in code, you have to add the code to the top of EVERY page on your site. And going forward you have to remember to add it to all new pages as well. It is MUCH more efficient to do this using some CPANEL 301 redirect or doing it directly in Mod Rewrite using .htaccess files. This way you write one RewriteRule in a .htaccess files that takes care of ALL pages on your site (existing and future pages).