Archive for category PHP
PHP Redirect 301 Permanently
Sometimes you might want to redirect your visitors to a new URL address. This article will show you how to make a PHP redirect using the 301 “moved permanently” redirection. This is the one you should use as it is the most search engine friendly. Like the name suggests, PHP redirect tells the browser (or a search engine bot) that the page has been permanently moved to a new location.
PHP Redirect Code
To redirect people and robots to a new location use this PHP redirecting code:
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.New-Website.com");
You could skip the 301 Moved Permanently tag and use just:
header("Location: http://www.New-Website.com");
But this would result in a “302 Moved Temporarily” redirect instead of a 301 one. This should be avoided as permanent redirects are more search engine friendly and should be used where possible.
You can enter any sub-page for the location, this PHP code will redirect users to the test.php sub-page of your website:
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.New-Website.com/test.php");
It is important that you don’t have any other code (including empty rows and spaces) before the above PHP redirect code. If you do you will get a nice headers already sent notice from PHP and the redirect will not work.
That’s it! Enjoy redirecting PHP pages.
Compressing Stylesheets with PHP
Posted by bekco in PHP, Programming Languages, Software Engineering, Web Design on June 21, 2011
If you wonder what else you can do to compress your files, I found a PHP code of which is written by a German coder that is shared on another blog. Basicaly it removes spaces and back slashes to minify the included files and compress them using ob_gzhandler.
header('Content-type: text/css'); ob_start("compress"); function compress($buffer) { /* remove comments */ $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); /* remove tabs, spaces, newlines, etc. */ $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); return $buffer; } /* your css files */ include('master.css'); include('typography.css'); include('grid.css'); include('print.css'); include('handheld.css'); ob_end_flush();
Compressing Web Site Contents, Scripts and Stylesheets
Posted by bekco in Javascript, PHP, Programming Languages, Software Engineering, Web Design on June 21, 2011
Today I wonder how I could minify my javascript files and do a server side compression to minimize the cost of loading web site requred javasript and stylesheet files. Where to begin… I’ll start from Apache Server configuration information for server side compression and finish with YUI Compressor that helps you to minify your script files as you should have seen those files ending with “your_script_file.min.js”. If you are using Apache Server, you might have Deflate Module installed with your Apache installation. What you need to load this module while your your server starts is to configure your apache configuration file.
LoadModule deflate_module modules/mod_deflate.so
And if you have access to the httpd.conf
then you can add following lines to it.
<Location /> AddOutputFilterByType DEFLATE text/html text/plain text/xml text/x-js text/css </Location>
If not; you should specify the initialization line into your htaccess file.
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css text/plain text/javascript text/xml application/xhtml+xml application/x-httpd-php
</IfModule>
You might have problems to compress javascript files which has different mime types. Then, you can use followig configuration.
AddOutputFilterByType DEFLATE application/x-javascript
That will do. Now, I will introduce YUI Compressor. YUI Compressor is one of Yahoo developer team product that you can download from here. If you have dowloaded the YUI Compressor built from the site that I shared above, what you should do to compress your file is:
java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js --charset utf-8
Where, x, y and z are the version number of the YUI Compressor download. Also you can find more information about YUI Compressor at http://developer.yahoo.com/yui/compressor/#work
Thank you for reading, I hope this will help some of you guys.