1

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!

3 Answers 3

1

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]
Sign up to request clarification or add additional context in comments.

Comments

0

Try this out :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]

Comments

0

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 RewriteRules. By default RewriteConds 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" }

1 Comment

@atoms, can you provide what you want to happen if the link is localhost/styles/foo? This is addressed to your answer, but I can't comment on your answer, so bear with me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.