Wednesday 23 March 2011

Simple url rewriting, using Apache mod_rewrite

Some customers want to have nice, fancy, search engine friendly url-s in their browser, on their pages.
That is completely reasonable because it helps to better position the pages in search results.

For instance:


www.somewbsite.com/page1.php?subject=geography&field=cont1


or

www.someanotherwebsite.net/about_us.html

is not so tasty for search engine bots like the following:


www.somewbsite.com/geography/continents/chapter-one


or


www.someanotherwebsite.net/about-us


If your web site runs on Apache web server than you could do it like this:


Create .htaccess file if there is not one already.

Write simple rule.

Write rule using regular expressions.


1. .htaccess file


Just a snapshot of some other code, you start from an empty .htaccess file


Look for it in your server root folder, public_html or htdocs. Open it using your text editor and put on a first line:

RewriteEngine on


Let us use it on above mentioned example:

2. Simple rule

Add this to .htaccess file:

RewriteRule ^about-us$ about_us.html


The part between ^ and $ is your url how you want it to be shown. The rest of the line is the actual file name of a document to be opened in browser.


3. A bit more power...


If you have url queries like in the first ugly url, you must use regular expressions:


RewriteRule ^([a-z]+)/([a-z]+)/chapter-one$ page1.php?subject=$1&field=$2


[a-z] matches all characters between lowercase a and z.
+ sign means any number of ocurrences, and brackets () define a variable to be used later in the rule.
So first ([a-z]+) in our example, matches 'geography' and the second one matches the word 'continents'.
These two variables are referenced in the real url by using $1 and $2. Numbers are assigned by the order of variable appearance.


If it does not work, perhaps you don't have mode_rewrite module enabled on your server. You can check that by executing a php file that contains only one function:

<?php
phpinfo();
?>


Under 'Loaded Modules' section look for mod_rewrite. If it is not listed there, you have to enable it by removing comment from corresponding line in httpd.conf file and restarting Apache server.
If you don't have access to httpd.conf then ask your hosting service to enable the module.

Saturday 12 March 2011

How to make a simple web page

You need:

- web browser (Firefox, Internet Explorer, Safari or something alike)
- plain text editor (Notepad, Vim, Arachnophilia,...).

The most important part of a web page is HTML document. That is a text document containing page formatting code (HTML tags) and page items (texts, pictures etc...).

Let us assume you use Notepad which is bundled with any version of Windows.

General plan:
Make simple HTML document using some common basic tags. Choose Notepad text editor as main tool now.
Fill the document with some texts and an image.
Add link to another page.
Open it in web browser to see how it works.



Procedure:

1. Create document file

Open Notepad text editor:
(Start->All programs->Accessories->Notepad).
It will show you a blank untitled document. Save it first:
File -> Save as
Now you see a Save As dialog box. It has a field labeled "File name".

Name your document as my_page.html

IMPORTANT:
You have to type name of the document enclosed in quotation marks, like this: "my_page.html".
Otherwise, Notepad will name it like my_page.html.txt and it wont work.

Then you click Save button.

2. Start coding

Make sure you remember where you saved your document, so you can find it later.

Now, We'll start to write code:
You begin with <html> tag, it marks the beginning of a HTML document.
HTML tags are special markings enclosed between angle brackets, like so: <html>, <body>, <h1>, <h2>, etc...

Second tag is <head>. Head section contains code that does not appear on a visible part of a web page, it has some special purpose.

After that, give your page a title: <title>This is my first web page</title>.  Almost all web tags have it's closing tag. It has an extra slash character, like so: </html>,</title>, </body>, </h1> ...
Close the head section: add closing </head> tag.

3. Page content

Now comes the part of putting actual page content, what is supposed to be seen on the page.
Open body tag: <body>
Your code should look like this so far:

<html>
<head>
<title>
This is my first web page
</title>
</head>
<body>


Let's put some text.

Text is often formatted as paragraphs. Put tag to open new paragraph: <p>.

You can write some text of your own, or copy/paste it from somewhere. I'll use dummy text used for making page templates. You can find it on internet by googling "Lorem ipsum".
Paste it after <p> tag.
Then add closing </p> tag to close that paragraph.

4. Preview in browser

Your page has some content so we could give it a try and see how it looks like in a browser.

But before that we have to close body and html tag.
Add closing </body> tag after the closed paragraph.
Then add closing </html> tag.

Save the document: File->Save

Find the document where you saved it (Desktop, My Documents or else), double click on icon and, voila, you see it in a web browser.
On the very top of browser window, you can see the page title you have defined using <title> tags.

Anyway, not much of a beauty to see.

Let's add some more elements to it.

5. Add text title.

Before the opening <p> tag, add opening <h1> tag. It is often used to make a heading title on a page. Write some title for your paragraph. I'll put "What is lorem ipsum" .

Save it again and refresh your page in browser: Ctrl+F5 (reload) to see the added title.

Your code so far should look like this:

<html>

<head>

<title>
This is my first web page
</title>

</head>

<body>

<h1>What is Lorem Ipsum</h1>

<p>

Lorem Ipsum is simply dummy text of the printing and typesetting  industry. Lorem Ipsum has been the industry's standard dummy text ever  since the 1500s, when an unknown printer took a galley of type and  scrambled it to make a type specimen book. It has survived not only  five centuries, but also the leap into electronic typesetting,  remaining essentially unchanged. It was popularised in the 1960s with  the release of Letraset sheets containing Lorem Ipsum passages, and  more recently with desktop publishing software like Aldus PageMaker  including versions of Lorem Ipsum.
</p>

</body>

</html>

6. How to add image

You need to use <img> tag to display images.

Put it after the closing </p> tag.
First, you need the image file. Copy some file of your choice to the same place where your "my_page.html" document is (Desktop, My Documents, or else). Image file should be of type .jpeg, .gif or .png. Today, mostly used image formats on web pages are JPEG and PNG.
You can check it out by looking at file properties: right click on file icon, then click Properties and see what kind of image file you have.
Change <img> file in the following way: <img src="moon.jpg">
My image happens to be an image of the Moon and it's file is named in that manner: moon.jpg

You should find out what is your file named and what is it's extension. Extension is a part of a file name that comes after the dot.
Image files usually have .jpeg, .jpg, .gif, etc. extension. Check it out by looking at file properties.
After you put exact file name of the image including it's extension in image tag's attribute src="image_file_name.file_extension", refresh your document in the browser (Ctrl+F5) and there is your web page with an image.

7. Link it to another page.


Links are something common to see and use on a web page. It is created by putting anchor tags:
<a href="">Link text</a>

The main part is href attribute. It contains web address of a page you want to link to. I'll use Wikipedia.
So put this under the image tag:
<br />
Break tag <br /> makes break on a page, so the next part starts in a new row.
After the break tag add the link code:
<a href="http://wikipedia.org">Wikipedia</a>
http:// part is essential for browser to recognize that it should look for the linked address at internet.

Refresh page and that is it. Click on the link.
 You have web page at your computer, made by you.
Add more texts, images and links to experiment.

Next time I'll show you how to put it on internet.
The complete code:


<html>

<head>

<title>
This is my first web page
</title>

</head>

<body>

<h1>What is Lorem Ipsum</h1>

<p>
Lorem Ipsum is simply dummy text of the printing and typesetting  industry. Lorem Ipsum has been the industry's standard dummy text ever  since the 1500s, when an unknown printer took a galley of type and  scrambled it to make a type specimen book. It has survived not only  five centuries, but also the leap into electronic typesetting,  remaining essentially unchanged. It was popularised in the 1960s with  the release of Letraset sheets containing Lorem Ipsum passages, and  more recently with desktop publishing software like Aldus PageMaker  including versions of Lorem Ipsum.
</p>
<img src="moon.jpg">
<br />
<a href="http://wikipedia.org">Wikipedia</a>
</body>

</html>

Tuesday 8 March 2011

Invisible trouble

Perhaps you never encountered this. Happened to me once before and today I managed to fix it.
We use CodeIgniter PHP Framework as main tool in our work on this particular project. Me and my co-workers are developing rather huge website.

I am not going to write it's url here, because it is not fully online yet and I would be embarrassed to let you see all it's flaws ;-)  

The main page (home), on it's top, started to display some error messages like "Headers already sent ... blah, blah" and mentioned Session.php library.

I know that it like happens if one would print or echo something in the same method containing the PHP header() function later.

But there was not supposed to be any header() statements on that page/method.

I googled up some tips about it. The most significant advice was that there are some space characters outside of opening or closing PHP tags in a controller.

Notepad++ did not show any spaces there. Nor did the regular Notepad show anything.

This is a kind of thing that makes my self esteem disappear, in seconds :-/ Don't know when or if I am going to fix it or when.

Then I remembered to try again but using GVim. I was not surprised to see some one or two strange characters on the beginning of controller file, because it was the only logical cause for this error.

It also proved to me that I should use GVim as a main editor.

Why this happened? I suppose that is because different text editors open, save and close files in a non consistent manner, or do it in a different way.