I currently use $_GET['base']
to determine which homepage that the user visits.
This results in localhost/?base=administrator
or localhost/?base=guest
I am also using this to control which page is the user at, such as localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works? Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest
to localhost/guest
localhost/?base=admin
to localhost/admin
localhost/?base=guest&page=register
to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case
loop to include which items are needed.
This, however, works for localhost
, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L] RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register
& localhost/?base=administrator
will match the rule RewriteRule ^([^/]*)$ /?base=$1
.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register
. It won't match the first rule for localhost/?base=administrator
. I hope that helps.
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w
will match symbols a-z
, 0-9
and underscore _
, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base
and page
parameters in the $_GET
array
UPDATE:
to disable query string params page
and base
(other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]