| Subcribe via RSS

mod_rewrite domain

May 21st, 2008 | No Comments | Posted in anything under the moonlight, apache, htaccess by dreamluverz


RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

Introduction to mod_rewrite

Tags: ,

domain redirection

May 20th, 2008 | No Comments | Posted in SEO, apache, server by dreamluverz

As far as SEO is concerned 301 redirect is the most effecient way of domain redirection.



Some ways of doing it:

PHP Redirect

<?
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.new-url.com” );
?>

.htaccess

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

Check out here for other languages

other reference : http://www.somacon.com/p145.php

Tags: ,

error 500

May 8th, 2008 | No Comments | Posted in apache, server by dreamluverz

problem: error 500 on xampp when using htaccess:
solution: Locate this file httpd.conf and uncomment this LoadModule mod_rewrite modules/mod_rewrite.sop and restart apache

Tags: , , ,

install apache as a service

February 15th, 2008 | No Comments | Posted in apache by dreamluverz

Running Apache as a Service

Apache can be run as a service on Windows NT. There is some highly experimental support for similar behavior on Windows 9x.

You can install Apache as a service automatically during the installation. If you chose to install for all users, the installation will create an Apache service for you. If you specify to install for yourself only, you can manually register Apache as a service after the installation. You have to be a member of the Administrators group for the service installation to succeed.

Apache comes with a utility called the Apache Service Monitor. With it you can see and manage the state of all installed Apache services on any machine on your network. To be able to manage an Apache service with the monitor, you have to first install the service (either automatically via the installation or manually).

You can install Apache as a Windows NT service as follows from the command prompt at the Apache bin subdirectory:

httpd -k install

If you need to specify the name of the service you want to install, use the following command. You have to do this if you have several different service installations of Apache on your computer.

httpd -k install -n "MyServiceName"

If you need to have specifically named configuration files for different services, you must use this:

httpd -k install -n "MyServiceName" -f "c:\files\my.conf"

If you use the first command without any special parameters except -k install, the service will be called Apache2 and the configuration will be assumed to be conf\httpd.conf.

Removing an Apache service is easy. Just use:

httpd -k uninstall

The specific Apache service to be uninstalled can be specified by using:

httpd -k uninstall -n "MyServiceName"

Normal starting, restarting and shutting down of an Apache service is usually done via the Apache Service Monitor, by using commands like NET START Apache2 and NET STOP Apache2 or via normal Windows service management. Before starting Apache as a service by any means, you should test the service’s configuration file by using:

httpd -n "MyServiceName" -t

You can control an Apache service by its command line switches, too. To start an installed Apache service you’ll use this:

httpd -k start

To stop an Apache service via the command line switches, use this:

httpd -k stop

or

httpd -k shutdown

You can also restart a running service and force it to reread its configuration file by using:

httpd -k restart

By default, all Apache services are registered to run as the system user (the LocalSystem account). The LocalSystem account has no privileges to your network via any Windows-secured mechanism, including the file system, named pipes, DCOM, or secure RPC. It has, however, wide privileges locally.

Never grant any network privileges to the LocalSystem account! If you need Apache to be able to access network resources, create a separate account for Apache as noted below.

You may want to create a separate account for running Apache service(s). Especially, if you have to access network resources via Apache, this is strongly recommended.

  1. Create a normal domain user account, and be sure to memorize its password.
  2. Grant the newly-created user a privilege of Log on as a service and Act as part of the operating system. On Windows NT 4.0 these privileges are granted via User Manager for Domains, but on Windows 2000 and XP you probably want to use Group Policy for propagating these settings. You can also manually set these via the Local Security Policy MMC snap-in.
  3. Confirm that the created account is a member of the Users group.
  4. Grant the account read and execute (RX) rights to all document and script folders (htdocs and cgi-bin for example).
  5. Grant the account change (RWXD) rights to the Apache logs directory.
  6. Grant the account read and execute (RX) rights to the Apache.exe binary executable.
It is usually a good practice to grant the user the Apache service runs as read and execute (RX) access to the whole Apache2 directory, except the logs subdirectory, where the user has to have at least change (RWXD) rights.

If you allow the account to log in as a user and as a service, then you can log on with that account and test that the account has the privileges to execute the scripts, read the web pages, and that you can start Apache in a console window. If this works, and you have followed the steps above, Apache should execute as a service with no problems.

Error code 2186 is a good indication that you need to review the “Log On As” configuration for the service, since Apache cannot access a required network resource. Also, pay close attention to the privileges of the user Apache is configured to run as.

When starting Apache as a service you may encounter an error message from the Windows Service Control Manager. For example, if you try to start Apache by using the Services applet in the Windows Control Panel, you may get the following message:

Could not start the Apache2 service on \\COMPUTER
Error 1067; The process terminated unexpectedly.

You will get this generic error if there is any problem with starting the Apache service. In order to see what is really causing the problem you should follow the instructions for Running Apache for Windows from the Command Prompt.

There is some support for Apache on Windows 9x to behave in a similar manner as a service on Windows NT. It is highly experimental. It is not of production-class reliability, and its future is not guaranteed. It can be mostly regarded as a risky thing to play with - proceed with caution!

There are some differences between the two kinds of services you should be aware of:

  • Apache will attempt to start and if successful it will run in the background. If you run the command
    httpd -n "MyServiceName" -k start

    via a shortcut on your desktop, for example, then if the service starts successfully, a console window will flash up but it immediately disappears. If Apache detects any errors on startup such as incorrect entries in the httpd.conf configuration file, the console window will remain visible. This will display an error message which will be useful in tracking down the cause of the problem.

  • Windows 9x does not support NET START or NET STOP commands. You must control the Apache service on the command prompt via the -k switches.
  • Apache and Windows 9x offer no support for running Apache as a specific user with network privileges. In fact, Windows 9x offers no security on the local machine, either. This is the simple reason because of which the Apache Software Foundation never endorses use of a Windows 9x -based system as a public Apache server. The primitive support for Windows 9x exists only to assist the user in developing web content and learning the Apache server, and perhaps as an intranet server on a secured, private network.

Once you have confirmed that Apache runs correctly as a console application you can install, control and uninstall the pseudo-service with the same commands as on Windows NT. You can also use the Apache Service Monitor to manage Windows 9x pseudo-services.

source: http://httpd.apache.org/docs/2.0/platform/windows.html

Tags: , ,

error using mod_rewrite

January 24th, 2008 | No Comments | Posted in apache, server by dreamluverz

I got an error using mod_rewrite on my local server and it was only solved when I put  Options +FollowSymLinks but on live server using Options +FollowSymLinks gave me an error so I removed it. Well anyway, I came across this post and it has a nice explanation regarding that.

For reference, if anybody else wants to know:
mod_rewrite can be used to simulate a symbolic link. This is why mod_rewrite requires FollowSymLinks to be enabled, because it’s a similar security thing. So if your host doesn’t enable FollowSymLinks, and you try using mod_rewrite, you’ll get the 403 error. Adding Options +FollowSymLinks will override it for your directory, allowing mod_rewrite to work.

But, if you have this problem and you add Options +FollowSymLinks and then get a 500 error instead, then your host is disallowing the use of the Options directive in .htaccess files, and there’s little you can do but complain to them.

source : http://wordpress.org/support/topic/138301#post-630546

Tags: ,

debian - LAMP setup

January 19th, 2008 | 5 Comments | Posted in apache, mysql, php, server by dreamluverz

This tutorial has been helpful to me on my 1st attempt of setting up our dedicated server.

source: http://allyourtech.com/content/articles/16_01_2006_setting_up_a_local_web_server_in_debian_linux.php

The basic Linux setup

Setting this all up assumes you have a running version of Linux already installed. I won’t walk you through that part.

If you don’t have one and have an old box you’d like to use, I’d suggest using Debian as we are about to. But if you are new to Linux, you might want to try using a hard disk installation of Knoppix as the procedure will be identical.

Of course, you aren’t limited to Linux exclusively. All elements of LAMP (sans the Linux part) have been ported to most operating systems.

Setting up Apache

For the actual “web server” portion of our web server (i.e. the service sharing the web pages) we’ll be using Apache 2. In addition to being free and open source, Apache is by far the most used web server software on the Web.

Either dropping to a shell or connecting remotely, we’ll start by checking for updates to the Debian package lists. If you’ve done this recently or just did a fresh Internet-based installation, this won’t be necessary. This will require root access.

lineman@soundwave:~$ su
Password:
soundwave:/home/lineman# apt-get update
Get:1 http://security.debian.org stable/updates/main Packages [174kB]
Hit http://apt-mirror.sourceforge.net apt-mirror/ Packages
Ign http://apt-mirror.sourceforge.net apt-mirror/ Release
Get:2 http://security.debian.org stable/updates/main Release [110B]
Hit ftp://mirrors.kernel.org stable/main Packages
Hit ftp://mirrors.kernel.org stable/main Release
Hit ftp://mirrors.kernel.org stable/main Sources
Hit ftp://mirrors.kernel.org stable/main Release
Fetched 174kB in 3s (48.4kB/s)
Reading Package Lists... Done

Now that we’ve updated out package lists, we’ll tell Linux to go grab Apache. To fully install Apache, we’ll need more than one package. Thanks to Debian’s APT, we’ll be able to download and install all those dependencies with minimal work.

soundwave:/home/lineman# apt-get install apache2
Reading Package Lists... Done
Building Dependency Tree... Done
The following extra packages will be installed:
  apache2-common apache2-mpm-worker apache2-utils
Suggested packages:
  apache2-doc lynx www-browser
The following NEW packages will be installed:
  apache2 apache2-common apache2-mpm-worker apache2-utils
0 upgraded, 4 newly installed, 0 to remove and 5 not upgraded.
Need to get 1097kB/1131kB of archives.
After unpacking 4018kB of additional disk space will be used.
Do you want to continue? [Y/n] y

Although it isn’t a requirement, APT suggested installing the Apache documentation. So we’ll go ahead and install that just in case. It suggested Lynx too but we probably won’t be needing that.

soundwave:/home/lineman# apt-get install apache2-doc
Reading Package Lists... Done
Building Dependency Tree... Done
The following NEW packages will be installed:
  apache2-doc
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 0B/3861kB of archives.

Now we’ll do a quick check to verify Apache is running.

soundwave:/home/lineman# /etc/init.d/apache2 start
Starting web server: Apache2httpd (pid 11205) already running

So it looks like Apache is running, but let’s not take that scripts word for it. I mean, we’re setting up a web server so why not use a web browser? To do this, just plug the IP address of your server into the address box of your favorite browser. This should forward you to http://servername/apache2-default/ where you’ll find a page congratulating you on your successful Apache installation.


If you’d like to start by editing this sample page, it’s easy enough to do. The default Apache files are stored in /var/www/apache2-default on your server.

To edit the English version of the page, we’ll just have to use Nano (or another text editor) to edit /var/www/apache2-default/index.html.en.

soundwave:/home/lineman# nano /var/www/apache2-default/index.html.en


If you’d like to play with a few of Apache’s settings, the main configuration file is /etc/apache2/apache2.conf. Since by default Debian will allow external connections to Apache, you may want to limit connections to your localhost or local network only.

If you’d like to make web folders for individual accounts, all that is needed to make a directory in the user’s home directory called /public_html. For example, if your username is “lineman,” you’ll need to mkdir /home/lineman/public_html. Files stored in that directory will then be viewable in your web browser at the address http://servername/~lineman/.

At this point, if all you want to work on and host are static pages, you are done. But for those who want to do more interesting things using the LAMP platform, we’ll move on.

Setting up MySQL

There are plenty of Apache-friendly database servers available, but MySQL is certainly a popular option. Again, this one is free and open source.

We’ll start by installing MySQL in a manner similar to the way we installed Apache.

soundwave:/home/lineman# apt-get install mysql-server
The following extra packages will be installed:
  libdbd-mysql-perl libmysqlclient12 mysql-client mysql-common
Suggested packages:
  mysql-doc
The following NEW packages will be installed:
  libdbd-mysql-perl libmysqlclient12 mysql-client mysql-common mysql-server
0 upgraded, 5 newly installed, 0 to remove and 5 not upgraded.
Need to get 4523kB of archives.
After unpacking 10.5MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Once MySQL begins to install, we’ll get a brief warning screen. This is mainly to fill us in a little on how passwords will work. By default, the root user MySQL has no password (something you will want to change for a public server).


Before moving on, we’ll make sure the MySQL service is running.

soundwave:/home/lineman# /etc/init.d/mysql start
Starting MySQL database server: mysqld...already running.

If you’d like to change some of MySQL’s settings from their default values, take a look at the files located in /etc/mysql.

As with Apache, APT suggests we install the MySQL documentation. Just to be safe, we’ll go ahead and do that.

soundwave:/home/lineman# apt-get install mysql-doc
Reading Package Lists... Done
Building Dependency Tree... Done
Package mysql-doc is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package mysql-doc has no installation candidate

Unfortunately, this package has been removed from Debian. So if we want the documentation, we’ll have to grab the documents directly from MySQL.com.

If you are already comfortable using MySQL from the command line, you can probably skip the section on phpMyAdmin further down. But if you aren’t, phpMyAdmin will make your life much easier.

Setting up PHP

While HTML is a useful markup language, it lacks the sophisticated scripting ability that the modern Web user expects. This is where scripting languages like JavaScript, ASP, and PHP are needed. In particular, server-side scripts like those used in PHP and ASP provide a framework for building security-minded Web interfaces.

It is with good reason that most Linux-based hosts use PHP over ASP (and not because LAMA just doesn’t sound all that desirable). PHP is a completely free and open source operating system. Cost aside, there are security considerations here. PHP has certainly not had a spotless record of security, but flaws are found much more quickly and are fixed almost immediately.

Now if you really want to use ASP, Apache can be made to support it. However, support is limited.

Most of us, though, are using PHP (hence the popularity of LAMP) so we’ll go ahead and set it up. Again, this starts on the command line.

soundwave:/home/lineman# apt-get install php4
Reading Package Lists... Done
Building Dependency Tree... Done
The following extra packages will be installed:
  apache-common libapache-mod-php4
Suggested packages:
  apache apache-ssl apache-perl php4-pear
The following NEW packages will be installed:
  apache-common libapache-mod-php4 php4
0 upgraded, 3 newly installed, 0 to remove and 5 not upgraded.
Need to get 2460kB of archives.
After unpacking 6357kB of additional disk space will be used.
Do you want to continue? [Y/n] y

Since we will be using both PHP and MySQL on our server, we’ll go ahead and install the PHP’s MySQL module to help them play together better. We’ll also install PHP’s Pear extensions since so many packages will require it.

soundwave:/home/lineman# apt-get install php4-mysql php4-pear
Reading Package Lists... Done
Building Dependency Tree... Done
The following extra packages will be installed:
  php4-cli
Suggested packages:
  php4-dev
The following NEW packages will be installed:
  php4-cli php4-mysql php4-pear
0 upgraded, 3 newly installed, 0 to remove and 5 not upgraded.
Need to get 1860kB/1882kB of archives.
After unpacking 5095kB of additional disk space will be used.
Do you want to continue? [Y/n] y

There are a few other packages we’ll need to install to get Apache working with PHP.

soundwave:/home/lineman# apt-get install libapache2-mod-php4 php4-cgi
Reading Package Lists... Done
Building Dependency Tree... Done
The following extra packages will be installed:
  apache2-mpm-prefork
The following packages will be REMOVED:
  apache2-mpm-worker
The following NEW packages will be installed:
  apache2-mpm-prefork libapache2-mod-php4 php4-cgi
0 upgraded, 3 newly installed, 1 to remove and 5 not upgraded.
Need to get 5024kB of archives.
After unpacking 9441kB of additional disk space will be used.
Do you want to continue? [Y/n] y

All the necessary packages are installed, but we aren’t quite done yet. Using your favorite text editor, we’ll need to make some changes to /etc/apache2/apache2.conf. First, locate the line “#AddType application/x-httpd-php .php” and un-comment the line by removing the # from the beginning. Next, we’ll need to add the line below.

LoadModule php4_module /usr/lib/apache2/modules/libphp4.so

To get those changes to take effect, we’ll have to restart Apache. If we don’t, web browsers will be downloading our full PHP files rather than Apache processing them first.

soundwave:/home/lineman# /etc/init.d/apache2 restart

To show that PHP is working, we’ll make a small PHP file and stick it in our web folder. Within the directory /home/lineman/public_html, I’m going to add a file called index.php with the following contents:

<?php
  phpinfo();
?>

This will generate a PHP page at http://servername/~lineman/ that will tell us a lot about our PHP installation.


Setting up phpMyAdmin

Don’t listen to the people who’ll tell you that you are cheating if you don’t run MySQL from the command line. Not only is that method more difficult, but you risk doing more damage if you make a mistake.

phpMyAdmin is a web interface designed specifically for administering MySQL. While there are times that MySQL is best used directly (like when you are moving a 50 MB database to a new co-located server), phpMyAdmin is commonly used to handle day-to-day database issues.

The installation of phpMyAdmin is fairly straightforward.

soundwave:/home/lineman# apt-get install phpmyadmin
Reading Package Lists... Done
Building Dependency Tree... Done
Suggested packages:
  php4-gd php5-gd
The following NEW packages will be installed:
  phpmyadmin
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 2768kB of archives.

Once the installation begins, we’ll be presented with a question about which web servers to reconfigure. Since we used Apache2, be sure to check that. When prompted, go ahead and have Apache2 restarted.


To test out phpMyAdmin, we’ll first point our web browser to http://servername/phpmyadmin/. We will connect using the username “root” and a blank password. Once we do, phpMyAdmin will greet us with stern warning about our blank password. Clicking on “Change password” will allow us to set one and get rid of that warning.


Conclusion

There you have it – your very own web server to tweak and test on without fear of downtime for your own or your clients’ websites! But I have to warn you about one thing; once you’ve learned all the things you can do on your own server that your shared host isn’t letting you have access to, you’ll almost certainly be shelling out extra cash every month for your own dedicated server.

Tags: , ,

mod_rewrite and regular expressions

December 19th, 2007 | No Comments | Posted in apache, server by dreamluverz

I’ve been looking for this article and finally found it! Thanks to justin who posted this on some website.
Regular Expressions
I guess I should start by describing a regular expression. (They aren’t too scary once you get to know them.) A regular expression is basically a small piece of code that checks for patterns. The pattern can range from a single character that matches to absolutely everything. Regular Expression Pre-qualifier… these definitions are how regular expressions are generally used in .htaccess files and though most definitions will be applicable globally, there are some that may not. There are some predefined ‘terms’ in regular expressions to make your life easier. (At least, that are supposed to make your life easier.) Here is a short list, with what each does in the mod_rewrite setting.

[ ] enclose the expression or a portion of the expression. (Used for determining the characters, or range of characters to be matched.)

letter-letter (EG [a-z] matches any single lowercase alphabetical character in the range of a to z), so [c-e] will match any single character that is the lowercase letter c, d, or e.

LETTER-LETTER (EG [A-Z] matches any single capital alphabetical character in the range of A to Z), so [C-E] will match any single character that is the capital letter C, D, or E.

number-number (EG [0-9] matches any single number in the range of 0 to 9), so [4-6] would match any single number 4, 5, or 6.

character list (EG [dog123] matches any single character, either d, o, g, 1, 2, or 3.

^ has two purposes, when used inside of [ ] it designates ‘not’. (EG [^0-9] would match any character that is not 0 to 9 and [^abc] would match any character that is not a lowercase a, b, or c.) When used at the beginning of a pattern in mod_rewrite, it also designates the begining of a ‘line’.

It is very important to understand and remember [dog] does not match the word ‘dog’, it matches any individual lowercase letter d, o, or g anywhere in the comparison. in the same way, [^dog] does not exclude the word ‘dog’ from matching, it excludes the lowercase letter d, o, or g from matching individually.

To match a ‘word’ or a group of characters in order, you do not need to use [] so ^dog$ would match the word dog, and not d, o, or g as a single character.

. (a dot) matches any single character, except the ending of a line.

? matches 0 or 1 of the characters or set of characters in brackets or parentheses immediately before it. (EG a? would match the lowercase letter ‘a’ 0 or 1 time, (abc)? would match the phrase ‘abc’ 0 or 1 time, while [a-z]? would match any lowercase letter from ‘a to z’ 0 or 1 time.)

+ matches 1 or more of the characters or set of characters in brackets or parentheses immediately before it. (EG a+ would match the lowercase letter ‘a’ 1 or more times, (abc)+ would match the phrase ‘abc’ 1 or more times, while [a-z]+ would match 1 or more lowercase letters from ‘a to z’.)

* matches 0 or more of the characters or set of characters immediately before it. (EG a* would match the lowercase letter ‘a’ 0 or more times, (abc)* would match the phrase ‘abc’ 0 or more times, while [a-z]* would match 0 or more lowercase letters from ‘a to z’.)

These are the basic building blocks of regular expressions as used in .htaccess and associated with mod_rewrite. By themselves, they do little, but when you put them together, they become very powerful.

Along with regular expressions, mod_rewrite allows for the use of special characters. It’s a good thing to understand what these are before you begin writing rules. (Mainly because you need one or more of them in almost every rule.)

RewriteRule tells the server to interpret the following information as a rule.

RewriteCond tells the server to interpret the following information as a condtion of the rule(s) that are immediately after it.

^ defines the begining of a ‘line’ (starting anchor). Remember, ^ also designates ‘not’ in a regular expression, so please don’t get confused.

( ) creates a variable to be stored and possibly used later, and is also used to group text for use with the quantifiers ?, +, and * described above.

$ defines the ending of a ‘line’ (ending anchor), and when followed by a number from 1 to 9, also references a variable defined in the RewriteRule pattern (used for variables on the right side of the equation or to match a variable from the rule in a condition, see example below).

% references a variable defined in a preceding rewrite condition. (used for variables on the right side of the equation only, see example below)

*note* - The right side of the equation is everything that follows the $ in a RewriteRule.

Examples: All variables are given a number according to the order they appear; The following rule and condition each have two variables, defined by parenthesis, so to use them you would put them where you need them in the results:
(the ‘-’ is for spacing only to make the line more readable, and is not necessary to use variables.)

RewriteRule ^(var1)/no-var/(var2)$ /to-use-variables-type-$1-and-$2

The final result would look like this:
to-use-variables-type-var1-and-var2

RewriteCond %{CONDITION_STUFF} ^(var1)/no-var/(var2)
RewriteRule ^no-var/no-var/no-var$ /to-use-variables-type-%1-and-%2

The final result would look like this:
to-use-variables-type-var1-and-var2

To use a combination of the Condition and Rule Variables

RewriteCond %{CONDITION_STUFF} ^(var1)/no-var/(var2)
RewriteRule ^(var1)/no-var/(var2)$ /to-use-variables-type-$1-and-%2-$2

The final result would look like this:
to-use-variables-type-var1-and-var2-var2

The exception to the above examples is, you can also use the %{CONDITION_STUFF} server variables in the right side of a rule, but it must appear exactly as in the condition:
RewriteRule ^(var1)/no-var/(var2)$ /type-%{CONDITION_STUFF}

¦ (bar) stands for ‘or’, normally used with alternate text or expressions grouped with parenthesis (EG (with¦without) matches the string ‘with’ or the string ‘without’. Keep in mind that since these are inside parenthesis, the match is also stored as a variable.)

\ is called an escaping character, this removes the function from a ’special character’ (EG if you needed to match index.php?, which has both a . (dot) and a ?, you would have to ‘escape’ the special characters . (dot) and ? with a \ to remove their ’special’ value it looks like this: index\.php\?)

! is like the ^ in a grouped regular expression and stands for Not, but can only be used at the beginning of a rule or condition, not in the middle.

- on the right side of the equation stands for No Rewrite. (It is often used in conjunction with a condition to check and see if a file or directory exists.)

mod_rewrite Directives for URL Redirection

Flags, in mod_rewrite are what give you the control of the response sent by the server when a specific URL is requested. They are an integral part of the rule writing process, because they designate any special instructions that might be needed. (EG If I want to tell everyone a page is moved permanently, I can add R=301 to my rule and they will know.)

Flags follow the rule and the most often used, are enclosed with [ ] (Not all flags are covered here, but the main and widely used ones are.)

[R] stands for Redirect. The default is 302-Temporarily Moved. This can be set to any number between 300 and 400, by entering it as [R=301] or [R=YourNumberHere], but 301 (Permanently Moved) and 302 (Temporarily Moved) are the most common.

(If you just use [R] this will work, and defaults to 302-Temporarily Moved)

** Do not use this flag if you are trying to make a ’silent’ redirect.

[F] stands for Forbidden. Any URL or file that matches the rule (and condition(s) if present) will return a 403-Forbidden response to anyone who tries to access them. (Useful for files that you would like to keep private, or you do not want indexed prior to ‘going live’ with them.)

[G] stands for Gone. (Similar to 404-Not Found, but it indicates that a resource was intentionally removed.) Not recommended for use unless you test the HTTP protocol level used by the client and return 410-Gone only to HTTP/1.1 or enhanced HTTP/1.0 clients. Older true HTTP/1.0 clients will treat 410-Gone as 400-Bad Request.

[P] stands for Proxy. This creates a type of ’silent redirect’ for files or pages that are not actually part of your site and can be used to serve pages from a different host, as though they were part of your site. (DO NOT mess with copyrighted material, some of us get very upset.)

[nc] stands for No Case as applied to letters, so if you use this on a rule, MYsite.com, will match mysite.com… even though they are not the same case. (This can also be used with regular expressions, so instead of [a-zA-Z], you can use [a-z] and [nc] at the end of the rule for the same effect.)

[QSA] stands for Query String Append. This means the ‘query string’ (stuff after the?) should be passed from the original URL (the one we are rewriting) to the new URL.

[L] stands for Last rule. As soon as this flag is read, no other following rules are processed. (Every rule should contain this flag, until you know exactly what you are doing.)

in an attempt to put together regular expressions and mod_rewrite special characters here are some examples of what they do:

Goal: to match any lowercase words, or group of letters:
Possible Matches:
lfie, page, site, or information
Expression:
[a-z]+

Explanation: [a-z] matches any single letter. + matches 1 or more of the previous character or string of characters. When you put the two together you have a regular expression that matches any single letter from a to z over and over, until it runs into a character that is not a letter.

Goal: to match any words, or groups of letters, and store them in a variable:
Possible Matches:
lfie, Page, site, or inforMation
Expression:
([a-z]+) [nc]

Explanation: Same as above with the addition of () and [nc]. in mod_rewrite, () creates a single variable out of the regular expression, so the word matched is now in a variable. [nc] stands for ‘No Case’ (from mod_rewrite) specifying that the regular expression or regular text strings match both upper and lowercase letters. With this expression you can match any single word.

Goal: to match any word, or group of letters, then any single number, and store them in separate variables:
Possible Matches:
lfie1, Page2, site6, or inforMation9
Expression:
([a-z]+)([0-9]) [nc]

Explanation: Same as above, except notice there is no + in the number expression. This way, only a single number at the end will match. The letters are placed into one variable, and the number is placed into another.

Goal: to match any word, or group of letters, then any single number, and store them in the same variable:
Possible Matches:
lfie1, Page2, site6, or inforMation9
Expression:
([a-z]+[0-9]) [nc]

Explanation: Same as above, except notice the plus is immediately following (no space) the [a-z], but before the [0-9] (again no space), so the + affects the [a-z], but not the [0-9].

Goal: to match any word, or group of letters, then any group of numbers, and store them in the same variable:
Possible Matches:
lfie11, Page2, site642, or inforMation9987653
Expression:
([a-z]+[0-9]+) [nc]

Explanation: Same as above with the addition of a + immediately following to the numerical expression to match 1 or more numbers instead of only 1.

Goal: to match any word, or group of letters, any group of numbers, and any random letters and numbers, which might or might not be mixed together:
Possible Matches:
11, gPaE, s17ite642, or 2Createinfo4UisCool
Expression:
([a-z0-9]+) [nc]

Explanation: The change here is to the regular expression grouping. Putting a-z and 0-9 in the same grouping followed by [nc] matches any combination of letters and numbers.

Goal: to match any word, or group of letters, then a single /, then any group of numbers, and store only the numbers in a variable.
Possible Matches:
lfie/10, gPaE/1, site/642, or CreateinfoUisCool/2474890
Expression:
[a-z]+/([0-9]+) [nc]

Explanation: Using the [a-z]+ without () matches the letters as usual. By putting the / outside of any expression, the only thing that will match is the exact character of /. Then using the ([0-9]+) again, stores any group of numbers in a variable.
Goal: to match anything before the / and store it in a variable, then match anything after the / and store it in a separate variable:
Possible Matches: lfie/10.html, gP..aE/1page_two.file, si-te/642-your-site, or
Createinfo/245390.php
Expression:
([^/]+)/(.+)

Explanation: Using two new forms of regular expressions, this is actually easier than it may seem. Making use of the ^(not) character, matches anything that is not a / and the () again saves it in a variable. Then using the same form as above, the single, exact character of / is matched. Finally, the . (dot) character is used, because it matches any single character that is not the end of a line, and when combined with the + character, matches anything up to a line break. Once again, () are used to create the variable. *Also, notice the use of a ‘catch-alls’ eliminates the need for the [nc] ‘flag’ of mod_rewrite.

Justin

source: http://www.webmasterworld.com/forum92/4332.htm

Tags: ,