Archive for May, 2008

htaccess Conditional Loop Redirect

Like the ‘if’ statement in many programming languages, you can use conditional rule to control mod_rewrite redirect. One very common use for RewriteCond is to prevent looping.

The following rule set causes any request starting with home to be redirected to /home.html. The resulting URL, however, starts with home and will therefore trigger the RewriteRule to be run again. The RewriteCond directive ensures that the rule will be skipped if the request is already for home.html and thus avoids this looping.

RewriteCond %{REQUEST_URI} !^/home\.html
RewriteRule ^home /home.html [R]

,

2 Comments

Useful mod_rewrite Resources

There are many free and useful mod_rewrite resources online.

Regex Tools
If you’re going to spend more than just a little time messing with regexes, you’re eventually
going to want a tool that helps you visualize what’s going on. There are a number of
them available, each of which has different strengths and weaknesses. You’ll find that
most of the really good tools for regular expression development come out of the Perl community, where regular expressions are particularly popular and tend to get used in
almost every program.

Regex Coach, which is available for Windows and Linux, and can be downloaded from http://www.weitz.de/regex-coach/. Like Rebug, Regex Coach allows you to step through a regular expression and watch what it does and does not match. This can be extremely instructive as you learn to write your own regular expressions.

Reference Sources
Because mod_rewrite is built on top of the Perl Compatible Regular Expression (PCRE) vocabulary, it is god to take a look at the Perl regular expression documentation, which you can find online at http://perldoc.perl.org/perlre.html or by typing perldoc perlre at your command line, and the PCRE documentation, which you can find online at http://pcre.org/pcre.txt.

,

1 Comment

Yahoo Music Search Pirate and China Earthquake

Yahoo! China Music Search is one of world’s biggest illegal music search engine. From May 19 to 21, even this Yahoo operated pirated music website is taking a break and joining the China national mourning, surmount earthquake relief!

Below is the original URL screen shot (click to see a bigger image):

Below is the Google Translated version (click to see a bigger image):

Yahoo is really a joke, I don’t see this temporarily halt of their dirty pirated music business can be in any way remotely linked to ‘national mourning’, and what’s more of a joke is that if this bizarre mourning is nothing but a pretend. Just use Yahoo’s ‘friend’ Google to do a search on ‘yahoo music china’, and click on the site map item of the first result… oops! I am in the pirate zone! See the image below:

Reject Microsoft’s offer is a brave thing to do, but Yahoo, you need to be smart, not just brave. I can’t believe the earthquake didn’t wake Yahoo up, it still comes out with this kind of idiot nonsense.

Yahoo’s action is a joke, but the earthquake itself is no joke, let’s hope the survivors could get their lives back to normal from this terrible disaster.

No Comments

Install Photoshop on Ubuntu

To install Photoshop CS2 or CS3 on Ubuntu is very easy. Photoshop doesn’t only work on Ubuntu, it actually performs better on Ubuntu than on Windows or Mac! Since the major Photoshop distribution targets Windows users, you first need to install Wine – an open source Windows compatibility layer. So before installing Adobe Photoshop, let’s first install Wine. To install and configure Wine, read the article Install and Configure Wine on Ubuntu first.

Ok, if you have successfully installed Wine, it’s time to install Photoshop, if you install Photoshop from a CD ROM, pop the installation CD for your Windows app in your drive. Open Terminal and type the command wine, followed by the path to the installer. NOTICE: Don’t use the default Setup.exe at root of the CD, which will run the autoplay, instead, run the setup.exe inside the Adobe(R) Photoshop(R) CS2 folder.

For example: wine "/media/cdrom0/Photoshop CS2/Adobe(R) Photoshop(R) CS2/setup.exe"

Or if you have downloaded the Flash Windows installation package somewhere like your desktop, you can also point to that location: ALSO NOTICE: Don’t use the default Setup.exe at root of the installation package which will run the autoplay, instead, run the setup.exe inside the Adobe(R) Photoshop(R) CS2 folder.

For example: wine "/home/shi/Desktop/Photoshop CS2/Adobe(R) Photoshop(R) CS2/setup.exe"

To launch the application, you will find it under: Applications -> Wine -> Programs -> Adobe Photoshop CS2

launch wine flash

you may need to restart your computer if it’s not appearing.

,

2 Comments

Install Flash CS on Ubuntu

To install Flash 8 or CS3 on Ubuntu is very easy. Flash doesn’t only work on Ubuntu, it actually works faster on Ubuntu than on Windows or Mac! Since the main Flash distribution targets Windows users, you first need to install Wine – an open source Windows compatibility layer. So before installing Flash, let’s first install Wine. To install and configure Wine, read the article Install and Configure Wine on Ubuntu first.

Ok, if you have successfully installed Wine, it’s time to install Flash, if you install Flash from a CD ROM, pop the installation CD for your Windows app in your drive. Open Terminal and type the command wine, followed by the path to the installer.

For example: wine "/media/cdrom0/Adobe Flash 8/flash8-en.exe"

Or if you have downloaded the Flash Windows installation exe somewhere like your desktop, you can also point to that location:

For example: wine "/home/shi/Desktop/Adobe Flash 8/flash8-en.exe"

To launch the application, you will find it under: Applications -> Wine -> Programs -> Macromedia -> Macromedia Flash 8

launch wine flash

you may need to restart your computer if it’s not appearing.

,

4 Comments

Install and Configure Wine on Ubuntu

Thanks to Wine, an open source Windows compatibility layer, Adobe’s Photoshop, Dreamweaver, and Flash all can be installed on Ubuntu.

Wine is not an emulator (hence the name, in true GNU recursive style), but it does provide an alternative, 100-percent-non-Microsoft implementation of the DLLs that Windows programs use. (Wine can also use actual Windows DLLs as well.) The software has been in development for more than 12 years and just reached beta status in the fall of 2005. But Wine has been in widespread use for quite some time, and it’s included in most distributions, including Ubuntu.

Install Wine

The Wine software included with Ubuntu is frequently at least a step behind the current version, so to run the latest version you’ll want to edit your /etc/apt/sources.list file and add Wine’s own apt repository. You can do so manually, or with the Synaptic Package Manager.

To add the line yourself, open Terminal and enter this command:

sudo nano /etc/apt/sources.list

After you furnish your password, the nano editor will open sources.list. Enter this line at the end of the file:

deb http://wine.sourceforge.net/apt binary/

Save the file (press Ctrl-O), then close, open Terminal again and run:

$ sudo apt-get update

This will update the package cache. Now you can install Wine with the command:

sudo apt-get install wine

Configure Wine

The first thing you will want to do with Wine is configure it. You do so by running the Wine configuration utility, winecfg. In Terminal, enter:

winecfg

It’s generally fine to accept the defaults, but you may find it helpful to add a new Windows drive (via the Drives tab) that explicitly maps to your CD-ROM drive. To do so, click the Add button to create a new Windows D drive, and then click Browse to select the path to your CD-ROM (such as /media/cdrom0/). Finally, click the Apply button to finish.

There’s one more thing you should attend to before you begin to install Windows apps, and that is to install Microsoft TrueType fonts (so applications running in Wine can render text correctly) and cabextract, a useful tool for extracting the contents of Microsoft .cab archives. You can install both packages via apt by issuing this command in Terminal:

sudo apt-get install msttcorefonts cabextract

Now your Wine is properly set up!

,

11 Comments

Validate URL Using PHP Regex

There are times you may want a URL validation function that accepts (I think) most known types of URL’s. Useful for validating a homepage link, or submission of links from the public.

It allows for port, path and query string validations, the parameter $url string contains the user input URL and the function returns the boolean of true or false.

<?php
/**
* Validate URL
* Allows for port, path and query string validations
* @param    string      $url	   string containing url user input
* @return   boolean     Returns TRUE/FALSE
*/
function validateURL($url)
{
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
return preg_match($pattern, $url);
}

$result = validateURL('http://www.google.com');
print $result;
?>

Hope this helps!

12 Comments

Create Custom Error Pages Using htaccess PHP

Human creations are like ourselves – imperfect. The occurrence of error, whether caused by the surfer or by programmer, is inevitable. But it’s our job to do the damage control, the best thing to do when an error occurs is to redirect browsers that experience a 404 error (Not Found) to the file “error.php” located on the root of the server. With a little PHP coding you can set up this file to handle all error codes which will make them easier to manage.

The complete (and very long) list of errors is available here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Some of the most common errors you’ll probably want to make entries for are:

400 – Bad Request
401 – Unauthorized
403 – Forbidden
404 – Not Found
500 – Internal Server Error

.htaccess

To catch those errors, you would add the following to .htaccess for the domain you’d like to configure:

ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

error.php

Then, in error.php, add something like the following. This particular example is made for a wordpress site, and if someone goes to http://www.example.com/Foo and the page’s not found, they are redirected to the search result page http://www.example.com/index.php?s=Foo. Anything after the last “/” is assumed to be a search term they were trying to get to.

<!--p
// this is especially useful with error 404 to indicate the missing page.
$page_redirected_from = $_SERVER['REQUEST_URI'];
$server_url = "http://" . $_SERVER["SERVER_NAME"] . "/";
$redirect_url = $_SERVER["REDIRECT_URL"];
$redirect_url_array = parse_url($redirect_url);
$end_of_path = str_replace("/", "", $redirect_url_array["path"]);

switch(getenv("REDIRECT_STATUS"))
{
	# "400 - Bad Request"
	case 400:
	$error_code = "400 - Bad Request";
	$explanation = "The syntax of the URL submitted by your browser could not be understood.";
	$explanation .= "Please verify the address and try again.";
	$redirect_to = "";
	break;

	# "401 - Unauthorized"
	case 401:
	$error_code = "401 - Unauthorized";
	$explanation = "This section requires a password or is otherwise protected.";
	$explanation .= "If you feel you have reached this page in error, ";
	$explanation .= "please return to the login page and try again, ";
	$explanation .= "or contact the webmaster if you continue to have problems.";
	$redirect_to = "";
	break;

	# "403 - Forbidden"
	case 403:
	$error_code = "403 - Forbidden";
	$explanation = "This section requires a password or is otherwise protected.";
  	$explanation .= "If you feel you have reached this page in error, ";
	$explanation .= "please return to the login page and try again,";
	$explanation .= " or contact the webmaster if you continue to have problems.";
	$redirect_to = "";
	break;

	# "404 - Not Found"
	case 404:
	$error_code = "404 - Not Found";
	$explanation = "The requested resource '" . $page_redirected_from . "'";
	$explanation .= " could not be found on this server.";
	$explanation .= "Please verify the address and try again.";
	$redirect_to = $server_url."?s=". $end_of_path;
	break;

	# "500 - Internal Server Error"
	case 500:
	$error_code = "500 - Internal Server Error";
	$explanation = "The server experienced an unexpected error.";
	$explanation .= "Please verify the address and try again.";
	$redirect_to = "";
	break;
}
-->

<!--CTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt-->

<!--p
	if (strcmp($redirect_to, "") != 0)
	{
-->

<!--p
	}
-->
<h1>Error Code <!--p print ($error_code);--></h1>
The <a href="http://en.wikipedia.org/wiki/Uniform_resource_locator">URL</a> you requested was not found. <!--P echo($explanation);-->

<strong>Did you mean to type <a href="http://www.blog.highub.com/wp-admin/%3C?php print ($redirect_to); ?>"><!--p print ($redirect_to);--></a>?</strong>
You will be automatically redirected there in five seconds.

You may also want to try starting from the home page: <a href="http://www.blog.highub.com/wp-admin/%3C?php print ($server_url); ?>"><!--p print ($server_url);--></a>
<hr />

<em>A project of <a href="http://www.blog.highub.com/wp-admin/%3C?php print ($server_url); ?>"><!--p print ($server_url);--></a>.</em>

2 Comments

Joomla Manually Setup Enable SEF URL

If you use Joomla! to develop website and know all the reasons to use SEF URL (Search Engine Friendly URL), then you must know that Joomla! has an option for you at the admin panel that allows you to check on three little radio buttons to enable this great function. That’s a perfect situation, but sometimes shit happens, and by following the instructions, you may get error messages like: JFTP::mkdir: Bad response, JFTP::chmod: Bad response, you tried chmod 777, but we all know it’s useless. So instead of trusting the three little buttons (which you can only end up in frustration), let’s on some homework by enabling it manually.

Before we make any changes, let’s make sure you got mod_rewrite enabled on your server first:

a) Type <?php phpinfo(); ?> in a php file and save it and run that file in the server.

b) And now you can the list of information, just search the word “mod_rewrite” from the browser’s search menu

c) If it is found under the “Loaded Modules” section then this module is already loaded as you see in the picture below, otherwise you need to go to the next step for enabling mod_rewrite module.

mod_rewrite

If you have mod_rewite installed, then go ahead and read the rest of the article, if you can’t find mod_write, contact your server host to enable it before any attempts.

1. No matter what kind of error messages Joomla! gives you, keep the three Joomla! SEO buttons enabled.

2. First if you have access to the server that hosts your website, connect to the sever, and at the root of your Joomla! installation, download the configuration.php file.

3. Open it up with any text editor you have, you may find something like the following lines:

var $sef = '0';
var $sef_rewrite = '0';
var $sef_suffix = '0';

Change the lines above to:

var $sef = '1';
var $sef_rewrite = '1';
var $sef_suffix = '1';

4. If your Joomla! installation is not at the base of the domain directory (for instance: if your website domain is at www.example.com, but you install joomla! website at www.example.com/en/), then you need to download your htaccess file and add a line like the one below:

RewriteBase /en

change en to the directory name of your own.

5. Upload the .htaccess file, remember if it’s htaccess.txt, change it to .htaccess.

Hope this helps!

8 Comments

PHP – Display all jpg Image Infomation

Use the following PHP function, you can display all image information (EXIF data) for a particular jpg file.

<?php
echo "aladdin-sane.jpg:<br />\n";
$exif = exif_read_data('david-bowie/aladdin-sane.jpg', 'IFD0');
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";

$exif = exif_read_data('david-bowie/aladdin-sane.jpg', 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
echo "$key.$name: $val<br />\n";
}
}
?> 

Hope it helps!

No Comments