views:

507

answers:

1

I am building a web app that puts out a link like the following:

http://www.sample.com/?a=bj7phm

I would like for it to look something like this:

http://www.sample.com/BJ7PHM

Is this possible within the HTACCESS?

-B

+1  A: 

In order to do URL rewriting, you first need to:

  • Make sure you have mod_rewrite enabled on your server.
  • Make sure you have the proper permissions to add rules to your .htaccess file.
    (AllowOverride must be set to All or include FileInfo)

Then create the following .htaccess file in your web root:

RewriteEngine On
RewriteRule ^([\-_0-9A-Za-z]+)$  index.php?a=$1 [L]

You can customize RewriteRule as much as you want.

The first parameter is the regular expression to match the REQUEST_URI with (relative to the folder the .htaccess is in).

The second parameter is what you want to rewrite it to, $n being your match groups.

Andrew Moore
There need to be a basic guide to mod_rewrites somewhere... Any links?
Chacha102
**@Chacha102:** Here is the official `mod_rewrite` documentation: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Andrew Moore
@Andrew Moore: Thanks for the quick insightful response. I am trying to get this to work right now and for some reason the code you displayed is not rewriting the URL. Could this have something to do with the link not actually including the text index.php?
Brad
**@Brad Birdsall:** Make sure that in `httpd.conf`, Apache has `mod_rewrite` enabled and that `AllowOverride` is set either to `All` or at least `FileInfo` for your root folder.
Andrew Moore
Not to ask a stupid question but how would I verify that? The server has a cPanel interface..would I find that in there?
Brad
**@Brad Birdsall:** Login into *WHM*. Use EasyApache to build Apache with `mod_rewrite`.
Andrew Moore