tags:

views:

30

answers:

2

Hello, i'm trying to set up a very simple rule in .htaccess file in order to rewrite this kind of url: www.domain.com/index.php?page=test to this: www.domain.com/test

I'm newbie to mod_rewrite, and so far, i came to this rule

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ^(.*)$ index.php?$1

but it doesn't work.

Any help? Thanks a lot

A: 

Almost right, you just forgot the page=:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1

Now note that this rule does the exact opposite: It rewrites requests of paths like /test internally to /index.php?page=test and not vice versa.

Gumbo
A: 

Another approach is:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([a-zA-Z0-9-_]+)/?$ index.php?page=$1.php [NC,L]

This also ensures your URL can only contain letters, numbers, dashes and underscores.

Altough, on a sidenote, I'd recommend you to have separate files for each page and including a header.php and a footer.php in those...

Claudiu
What's the purpose of FollowSymLinks here?
Chris Henry
Just to make sure that URL rewriting is active. Most of the time it is, but sometimes it isn't. No harm having it anyway.
Claudiu