Using query string (URL rewriting)

Using query string (URL rewriting)

  • The information sent across the web pages. 
  • This information is called a query string.
  • This query string can be passed from one page to another by appending its address of the page.
  • Passing more than one query string by inserting the & sign between the query strings.
  • A query string contains two things: the query string ID and its value.
  • The query string passed across the web pages is stored in $_REQUEST, $_GET, or $_POST variable.


Example:-  the query strings username and email (the names of textboxes) are passed from a page called login.php to another page called welcome.php when We click the submit button.

Login.php:
<html>
<head>
<title>Login form</title>
</head>
<body>
<form action="welcome.php" method=”get”>
<table>
<tr>
<td>User name:</td><td> <input type="text" name="username" ></td>
</tr>
<tr>
<td>E-mail: </td><td><input type="text" name="email" ></td>
</tr>
<tr>
<td><input type="submit" name="sub" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

welcome.php:
<?php
echo "<strong>Welcome ".$_GET['username']. "!</strong><br/>";
echo "Please remember this e-mail: ".$_GET['email']. " for later use.";
?>

The general formula for a rewrite rule is:

RewriteRule URL/to/match URL/to/use/if/it/matches [options]

URL rewriting has two things: redirecting the browser to a different URL, or rewriting the URL internally to use a particular file.

Redirects

Redirects match an incoming URL and then redirect the user’s browser to a different address. 

These can be useful for maintaining inheritance URLs.


Rewriting

By default, web servers closely map page URLs to the files in the website.

On receiving a request for http://example.com/about/history.html the server goes to the configured folder for the example.com website, and then goes into the about folder and returns the history.html file.

A rewrite rule changes that process by breaking the direct relationship between the URL and the file system. “When there’s a request for /about/history.html” a rewrite rule might say, “use the file /about_section.php instead.”

Matching patterns

the weird ^ and $ characters wrapped around the URL we’re trying to match. That’s because what we’re actually using here is a pattern. Technically, it is what’s called a Perl Compatible Regular Expression (PCRE) or simply a regex or regexp.

We’ve already met two regexp characters.

^

Matches the beginning of a string

$

Matches the end of a string

When a pattern starts with ^ and ends with $ it’s to make sure we match the complete URL start to finish, not just part of it. There are lots of other ways to match, too:

[0-9]

Matches a number, 0–9. [2-4] would match numbers 2 to 4 inclusive.

[a-z]

Matches lowercase letters a–z

[A-Z]

Matches uppercase letters A–Z

[a-z0-9]

Combining some of these, this matches letters a–z and numbers 0–9

The square brackets basically communicate the server to match from the selection of characters within them.

We can put any specific characters within the brackets, as well as the ranges.

all these just match one single character. [0-9] would match 8 but not 84 — to match 84 we’d need to use [0-9] twice. [0-9][0-9] So, if match 1984 we could to do this: [0-9][0-9][0-9][0-9]

That means any character between 0 and 9, four times.

If match a number, but didn’t know how long it (for example, a database ID in the URL) then use the + symbol, which means one or more. Ex. [0-9]+    This now matches 1, 123 and 1234567.

Capturing groups, and replacements

When rewriting URLs then matching and pass them along to the script that handles the request.

To do this, mark which parts of the pattern we want to reuse in the destination with round brackets or parentheses.

By placing parentheses around parts of the pattern want to reuse, called a capturing group.

To capture an important part of the source URL to use in the destination, surround it in parentheses.

pattern now looks like this, with parentheses around the parts that match the year and slug, but ignoring the slashes:

^([0-9]{4})/([a-z0-9-]+)/$

To use the capturing groups in the destination URL, use the dollar sign and the number of the group. So, the first capturing group is $1, the second is $2, and so on. (The $ is unrelated to the end-of-pattern $ we used before.)

RewriteRule ^([0-9]{4})/([a-z0-9-]+)/$ /abc.php?year=$1&slug=$2

The value of the year capturing group gets used as $1 and the abc title slug is $2. Had there been a third group, that would be $3 and so on.

In regexp idiom, these are called back-references as they refer back to the pattern.

Options

There are lots of options (or flags) set to change for the rule processing. The most useful (to my mind) are:

R=301

Perform an HTTP 301 redirect to send the user’s browser to the new URL. A status of 301 means a resource has moved permanently and so it’s a good way of both redirecting the user to the new URL, and letting search engines know to update their indexes.

L

Last. If this rule matches, don’t bother processing the following rules.

Options are set in square brackets at the end of the rule. set multiple options by separating them with commas:

RewriteRule ^([0-9]{4})/([a-z0-9-]+)/$ /abc.php?year=$1&slug=$2 [L]

or

RewriteRule ^about/([a-z0-9-]+).jsp/$ /about/$1/ [R=301,L]

URL rewriting is like a thin layer that sits on top of a website, translating human- and search-engine-friendly URLs into actual URLs. Doing it is easy because it requires hardly any changes to the website’s underlying structure — no moving files around or renaming things.

URL rewriting basically tells the Web server that /products/miniflow-gutter-brown/11577676 should show the Web page at: /diy/jsp/bq/nav.jsp?action=detail&fh_secondid=11577676, without the customer or search engine knowing about it.

================================================


Post a Comment

0 Comments