2

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.

5
  • Not entirely sure what the question is, but yes, .htaccess / mod_rewrite are often used to redirect / send people different places depending on parameters in the URL.
    – Epodax
    Jul 13, 2016 at 6:50
  • Tried a few of the mods, but doesn't really changes anything. Will update the question
    – Ctc
    Jul 13, 2016 at 8:02
  • 1
    @Ctc can you explain better what you want to achieve? for example: I want localhost/guest/register to redirect localhost/?base=guest&page=register something like this
    – George G
    Jul 13, 2016 at 8:06
  • @GeorgeGarchagudashvili Thanks for the suggestion. Edited as stated.
    – Ctc
    Jul 13, 2016 at 8:20
  • Welcome, I've posted answer you can check if it's working
    – George G
    Jul 13, 2016 at 8:37

3 Answers 3

1

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.

3
  • This made sense, but I received a error 500 afterwards.
    – Ctc
    Jul 13, 2016 at 8:11
  • Please make sure you don't copy the empty spaces, etc. from the above code I pasted. What URL you are trying to access which gives you a 500 error?
    – tovishalck
    Jul 13, 2016 at 8:13
  • accessing localhost will give a 500, localhost/guest will also return a 500
    – Ctc
    Jul 13, 2016 at 8:24
1

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]
3
  • This works as well, thanks for the comments, learnt slightly.
    – Ctc
    Jul 13, 2016 at 8:50
  • @starkeen: What does RewriteRule ^ - [L] do?
    – tovishalck
    Jul 13, 2016 at 9:54
  • This line says "Do not rewrite the uri" .
    – Amit Verma
    Jul 13, 2016 at 9:58
1

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]
6
  • This works when accessing localhost/guest and localhost/guest/register, but accessing localhost/?base=guest&page=register, it will still be the same, is there any way to redirect it completely? an upvote for the answer though.
    – Ctc
    Jul 13, 2016 at 8:49
  • I've updated it, but disabled only base, and page params from get var, but if you want we can disable query string at all (passing any param within url)
    – George G
    Jul 13, 2016 at 9:32
  • The incomming request is /?base= or /?base=foobar&page= , your pattern doesnt match the second one, also why are you redirecting it to 404?
    – Amit Verma
    Jul 13, 2016 at 9:47
  • @starkeen it works indeed, I'm redirecting to 404 because no direct access to /?base=foobar&page= something like this, only rewritten url works
    – George G
    Jul 13, 2016 at 10:35
  • Yes, it works. But instead of redirecting the real uri to 404 ,you can 301 redirect it back to the new url it's good for seo.
    – Amit Verma
    Jul 13, 2016 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.