Ironpaper Webintel: Knowledge base for internet business and web development.
Topic:

php

PHP 301 Redirect Code

Sunday, October 17th, 2010

Here is how to use PHP to create a permanent 301 redirect. This type of redirect tells user-agents that the webpage has moved permanently to the new, referenced location. Using 301 redirects can be a vital part of a search engine optimization (SEO) practice.

In order to use this code properly, you will need to place the PHP snippet above all content on the page. By placing the PHP 301 redirect code within or below other content will result in an error like the following: Warning: Cannot modify header information - headers already sent by...

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://test.com" );
?>

Please note: Replace "http://test.com" with your new location for the webpage.

301 Redirects In PHP

Thursday, July 22nd, 2010

This brief article will provide code samples for creating 301 redirects in PHP.

A 301 redirect is an easy and SEO friendly way to redirect a page that has changed address. For example, if you website recently changed Content Management Systems, and the new CMS generates a different URL construct, then you may want to redirect key pages so the user does not experience an (404) error.

A 301 redirect will also help preserve the PageRank assigned to that page prior to the move. So even if you aren't concerned about visitors stumbling upon a 404 error, you may want to at least tell Google that your page has moved.

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.example.com" );
?>

Also, redirect with location header:

<?php
header('Location:http://www.example.com');
exit;
exit();
?>

Or, redirect with “Refresh” header:

<?php
header('Refresh: 0; URL=http://www.example.com');
exit;
exit();
?>

How To PHP To Create A Facebook Like Button For A Dynamic WordPress Post Page URL

Saturday, June 19th, 2010

The Facebook Like Button is a very useful and rapidly popular social web marketing tool that can be applied to websites and webpages. By default, the Facebook's Like Button form will recommend a specific web page that is hard coded into the code snippet (which can be generated by Facebook's Like Button form).

Typepad users now have the Facebook Like Button embedded as a core application, which has helped to generate a lot of traffic to blog websites based on a recent report by Typepad.

It is possible to publish a Facebook Like Button that references a dynamic WordPress Post page rather than just making recommendations to the website in general.

How to reference the URL for a single WordPress post

On the WordPress post page template, below the snippet: <div <?php post_class() ?> id="post-<?php the_ID(); ?>">, you can print the page URL with the following code:

<?php the_permalink(); ?>

The above code snippet, will print the URL of that post page. However, within the Facebook Button invocation, you will need to take a different approach. Below is an example of the Facebook iFrame code for publishing a button without Profile faces to the WordPress dynamic URL post page. We indicated in bold the <?php ?> tag for printing the WordPress dynamic page.

<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80″ scrolling="no" frameborder="0″ style="margin-left: 30px; border:none; overflow:hidden; width:450px; height:30px;" allowTransparency="true"></iframe>

OR for printing HTML code version. Sorry, we will correct the output of quotes soon.

&lt;iframe src=&quot;http://www.facebook.com/plugins/like.php?href=&lt;?php echo urlencode(get_permalink($post-&gt;ID)); ?&gt;&amp;amp;layout=standard&amp;amp;show_faces=true&amp;amp;width=450&amp;amp;action=like&amp;amp;colorscheme=light&amp;amp;height=80&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;margin-left: 30px; border:none; overflow:hidden; width:450px; height:30px;&quot; allowTransparency=&quot;true&quot;&gt;&lt;/iframe&gt;