Ron Bodnar
Ron Bodnar

Reputation: 103

Apache Rewrite Rules for address bar location

I'm currently trying to make a rewrite rule which will show http://mywebsite.com/login/ in the address bar, but actually read from http://mywebsite.com/index.php?action=login. I've tried the following:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-z0-9\-]+)/?$ /?$.php?page=$1 [L,NC]
</IfModule>

The URL still shows up as http://mywebsite.com/index.php?action=login, though.

Basically, I want a SEO style links, I guess.

Upvotes: 0

Views: 442

Answers (1)

Book Of Zeus
Book Of Zeus

Reputation: 49885

Here's what you can use (static version):

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(index|blogs|projects|contact|login)/?$ index.php?action=$1 [L,NC]

As you can see, each "page" is hard-coded in the rule between the ().

You can also make it more "dynamic":

RewriteRule ^([a-z0-9\-]+)/?$ index.php?action=$1 [L,NC]

This will automatically use any letters (I added dash and numbers - like index, contact or contact-us) and use it as "action". So in your script you can check if one of these is not accepted (for example someone type: not-a-good-page) then you can redirect to a 404. This way, your script controls the logic not the .htaccess.

Upvotes: 3

Related Questions