1

.htaccess

    RewriteEngine On
    RewriteRule register index.php?mode=register
    RewriteRule login index.php?mode=login

index.php

    <?php
    if ( isset ( $_GET['mode']) && ( $_GET['mode'] == 'register' ) ) {
        include('includes/register.php');
    } elseif ( isset ( $_GET['mode']) && ( $_GET['mode'] == 'login' ) ) {
        include('includes/login.php');  
    }
    ?>

This is my current method (thanks to @TROODON).

Is there an easier way, maybe using key-value arrays to store all the possibilities for the various pages that index.php will call?

Thanks

1 Answer 1

3

For your .htaccess you can do this:

RewriteEngine ON

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

However, don't change your PHP code to just include whatever you are getting from $_GET['mode']! This will allow users to include at will.

You could adjust your PHP code like so:

$pages = array("register" => "includes/register.php",
               "login"    => "includes/login.php");

if(isset($_GET['mode']) && $pages[$_GET['mode']])
    include $pages[$_GET['mode']];

PS: The two RewriteCond's make sure the url is not an existing file or folder (i.e. if you have a folder images then site.com/images will still go to that folder instead of index.php?mode=images.

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

1 Comment

Sorry for the long delay in accepting this answer, I had to make sure it was up to scratch when I implemented it into my system, it works like a charm... so far! Thank you.

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.