domain

htaccess redirect function using mod_rewrite

Posted on November 06, 2009
1 Comment

continuation from previous post about htaccess redirect.

In more advance redirect, there’s a mod_rewrite module in apache that we can use to redirect with more options and rules. for more thorough documentation read here.

basically to use this redirect, there are some line that you need to put in your htaccess.
* RewriteEngine
* RewriteBase
* RewriteCond
* RewriteRule

Rewrite Engine

RewriteEngine on
Put this before all other rewrite commands to activate apache rewrite module.

Rewrite Base

RewriteBase /baseurl/
Put this to specify the base url for ur website, we use this if ur website is not located in the directly in the root domain folder (ex:your website is located at http://www.z3r0c0d3.com/blog) so you need to specify that the baseurl is blog. Put in RewriteBase /blog, so we can also use this value for later processing.
(example is to create a link, we can put baseurl in front of the link to link to other pages in the website.

Rewrite Rule

RewriteRule <Pattern> <Substitution> <[flags]>
This is the redirection rule that is really working to rewrite the request URL to a new address.
Take note that the order of this rule is important as we can implement combined rule to rewrite a request URL. Unlike the previous redirect command in htaccess, that simply redirect the request when the old_address matches. RewriteRule if the pattern matches, will first rewrite the old url to a new url with the substitution, then according to the flags given, continue to examine the next rule and apply second or more rewrite if the pattern matches. When all rewrite rules has been examined (or until Last flag is found) then the request url will then be transformed to the new url (or being redirected to the new url, according to the flag given).

the Pattern here is using regex (regular expression) to compare whether the request URL matches the pattern or not. important to take note is that we can save group of text in the old url and use it in the new url using () in the pattern, and use $N in the substitute.

example:
RewriteRule ^index.html$ another-link.html [R=301]
this will substitude http://blogs.z3r0c0d3.com/index.html to http://blogs.z3r0c0d3.com/another-link.html
and then redirect the request with 301 response code (defined in flag R=301)

RewriteRule ^.*$ http://www.google.com/another-link.html [R=301]
this will redirect all request in http://blogs.z3r0c0d3.com/ to www.google.com/another-link.html.

RewriteRule ^blog/(.*)/index.html$ another-blog/$1/index.php [R=301]
this will redirect requests in blog folder that ended with index.html, to another-blog folder and use the same path in old request before index.html, and put index.php behind.
http://domain.here/blog/1/2/3/4/asd/index.html will be redirected to http://domain.here/another-blog/1/2/3/4/asd/index.php

RewriteRule ^blog/(.*)/images/(.*)[gif|jpg|png|ico|bmp]$ images/$2jpg [R=301,L]
this will redirect all request to an image inside images folder folder in blog to a jpg image in images folder in root domain with the same filename.
http://domain.here/blog/1/2/3/images/some-picture.gif will be redirected to http://domain.here/images/some-picture.jpg

there are some flags that can be specified for the rule, some commonly used are
R=code which will redirect the request to the new url with specified code.
L is a flag to stop rewriting, (meaning don’t examine further rewrite rule sets) and that this is the last rule applied if match.
N means go back to the first rule and do the process all over again with the new url.
NC means to ignore case when matching the pattern.

Rewrite Cond

RewriteCond <TestString> <CondPattern>
Put one or sets of RewriteCond to help filter Request URL for a RewriteRule. So we can put one, two, or more RewriteCond and if all RewriteCond is matched then RewriteRule would be applied.

Some list that we can use for TestString:

HTTP_USER_AGENT
HTTP_REFERER
HTTP_HOST
REMOTE_ADDR
REMOTE_HOST
REMOTE_USER
REQUEST_METHOD
PATH_INFO
QUERY_STRING
SERVER_NAME
SERVER_ADDR
SERVER_PORT
THE_REQUEST
REQUEST_URI
REQUEST_FILENAME
THE_REQUEST
REQUEST_URI
REQUEST_FILENAME




CondPattern is a regular expression with some additionals:
“-d” is an existing directory
“-f” is an existing file
“!” continued by other CondPattern is to specify a non matching pattern.

example:
RewriteCond %{QUERY_STRING} .*
RewriteRule ^blog/(.*)$ another-blog/$1? [R=301,L]
the RewriteCond will check for every request with/without query string. Inside blog folder will be redirected (RewriteRule) to another-blog. We can put the “?” sign behind to remove the query string from the old url.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L]
the RewriteCond will check if the request is not an existing file, then rewrite the URL to index.php (but does no redirection because we didn’t specify R flag). so the url in address bar will stay the same, is just that the server will serve index.php to the browser. the RewriteRule won’t be applied if the request points to an existing file. (meaning it will serve the designated file if it’s exists, like images/css/js).



Tags: , , , ,

htaccess redirect function

Posted on November 06, 2009
No Comments

back to tutorial :-D
okay, for those who needs to manage a website, and moreover if u concern about search engine results, then eventually u will find the need to do a redirect.
what is a “redirect”?? in a simpler term, redirect is like when u move apartment, then u put a sign on the old apartment, “I move already to some address here“, then after reading the sign, ur guest will automatically be brought to ur new address. (in internet terms is called URL). So example you have a website, and then want to move to another domain, you can use this redirect function to automatically transport ur visitor to the new domain. no sweat! ^^

We can use a lot of method to do a “redirect”, and what i’m about to explain is using a “.htaccess” file in apache web server. There are two options, the advance one is using mod_rewrite function, and another one which is simpler is using redirect function. let’s start with the simpler one first:

redirect <code> <old_address> <new_address>

put this line in ur .htaccess file (usually htaccess file is located at the root folder of ur website, so that it can filter every request that comes to ur website, or alternatively u can put htaccess in each specific folder
in the public folder of ur website, take note though that placing .htaccess in the subfolder, will overwrite the rule defined in the root folder.

<code> is http response code to let the browser know why the request is being redirected. some commonly used code is: 301 (permanent move), 302 (temporary move), 404 (not found), 403 (forbidden)
<old_address> is the exact address/part of the address that u want to change with the new_address
<new_address> is the new address to replace the old_address

some example:
redirect 301 blog/ another_blog/ <–
this will redirect http://blogs.z3r0c0d3.com/blog/some_address to http://blogs.z3r0c0d3.com/another_blog/some_address

redirect 301 blog/ http://www.google.com/ <–
this will redirect http://blogs.z3r0c0d3.com/blog/some_address to http://www.google.com/some_address

redirect 301 blog/index.html blog/alternate.html <–
this will redirect http://blogs.z3r0c0d3.com/blog/index.html to http://www.google.com/alternate.html

if the old_address contains space (” ” or %20) you can put the old_address inside double quote.
redirect 301 “blog/some file name.html” blog/alternate.html <–
this will redirect
http://blogs.z3r0c0d3.com/blog/some file name.html to http://www.google.com/alternate.html
http://blogs.z3r0c0d3.com/blog/some%20file%20name.html to http://www.google.com/alternate.html

will continue the mod_rewrite in the next post.

Tags: , , , ,

blank – void – null – nothing – empty

Posted on November 07, 2008
No Comments
  • blank
  • void
  • null
  • nothing
  • empty
  • plain
  • confuse

hmm, choose from 5 options above that best describe ur feeling today. haha, yeah, u’re right, today i feel blank.. it’s nothing really, maybe tis because last night i slept at 3am. (yeah i read manga until 3am)
actually there’s some interesting happenings today..
the happy part: we had lunch in @ d office *free lunch!*, then we celebrate 4 of my colleagues birthday, jonathan create a simple facebook application! *yey* first hello world, next step *secret facebook apps* haha, yep, we discuss some interesting ideas about creating facebook apps.
then wely and jonathan get their free domain, moreover wely get a free hosting that has more space (800mb) than the one i’m using (250mb), the funny thing is, he’s free, i’m not! haha, can go to www.extremehost.com to get ur free hosting. ^^
the not so happy part: for next week, i am told to support some existing application, *sigh* i dun really like to support legacy application, meaning i need to read huge pile of code, get to understand the meaning behind those code, and bla bla.. hmm, but i’m quite used to it anyway, so i just told them “okay, can2..” with *not really interested, and we’ll see what happen next week* attitude.

Tags: , , , , ,