0

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.

2
  • You want to redirect only "home", "blogs" and "login"? Commented Feb 22, 2012 at 12:00
  • I want to redirect 'index', 'blogs', 'projects', 'contact', and 'login' - for now. Commented Feb 22, 2012 at 14:14

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

When I try to use $_GET['page']; it cannot find it when I try to load mysite.com/anything_here, is there a specific way I have to use it?
It should be: $_GET['action'], if you want to use page you can change the rule to: RewriteRule ^(index|blogs|projects|contact|login)/?$ index.php?page=$1 [L,NC]
I changed them around but it still won't accept my page parameter.
I changed /?$.php?page=$1 from your rule to: index.php?action=$1 and then I get in PHP (when i print_r(_$GET)): Array ( [page] => login ) for http://www.domain.com/login. Do you have any other rules in your apache config?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.