Reputation: 3093
I have a switch
on index.php
, which listens to ?action=
I would like to rewrite
anything after index.php/
to index.php?action=
Currently I have a rule to remove index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
So a current url looks like;
localhost:8888/?action=viewBasket
and would like to rewrite it to;
localhost:8888/viewBasket
Im getting confused as to wether I need to create a condition for each switch param, so to prevent images, script, sources etc from being rewritten. Or if there is a condition to check if exisits first. Cheers!
Upvotes: 1
Views: 119
Reputation: 171
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?action=$1 [L]
First line: Added this because I was scratching my head why it was not working for me. I'm sure you know you need this, just a note for others who might view this.
Second line: Checks if it is not a file.
Third line: Checks if it is not a directory. If it's a file or a directory, it will not process your RewriteRule
s. By default RewriteCond
s are operated with and.
Fourth line: I'm sure somebody can write a better rule. [L] last rule, stop evaluating RewriteRules. Tells it to rewrite to index.php?action={whatever} without changing the location header.
The gist of the fourth line is this: if your index.php is
var_dump($_GET);
www.example.com/foo
Will yield array(1) { ["action"]=> string(3) "foo" }
Upvotes: 0
Reputation: 2204
Try this out :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]
Upvotes: 0
Reputation: 3093
I have worked it out. Will remove my question tomorrow if no one provides a better soloution.
# redirect to any directories
RewriteRule ^styles/(.*) styles/$1 [L,QSA]
RewriteRule ^styles/fonts/(.*) styles/fonts/$1 [L,QSA]
RewriteRule ^images/(.*) images/$1 [L,QSA]
RewriteRule ^bower_components/(.*) bower_components/$1 [L,QSA]
RewriteRule ^scripts/(.*) scripts/$1 [L,QSA]
#if not a directory listed above, rewrite the request to ?action=
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]
Upvotes: 1