fanjavaid
fanjavaid

Reputation: 1738

.htaccess in multiple query string

i try use .htaccess to make clean URL. But i have problem in mutiple query string parameters.

I have three kinds of URL format like this :

 1. http://localhost/websekolah/main.php?page=register
 2. http://localhost/websekolah/main.php?page=detail_news&uid=10
 3. http://localhost/websekolah/main.php?page=gallery&hal=3

And i want my URL like this :

.    1. http://localhost/websekolah/register
     2. http://localhost/websekolah/detail_news/10
     3. http://localhost/websekolah/gallery/hal/3

I create this .htaccess but just only work for 1 query string

RewriteEngine On
RewriteRule ^[css|images|js|config|lib|pages] - [L,NC]
RewriteRule ^(.*)$ main.php?page=$1 [L,QSA]

I check my url redirection using this in main.php :

<?php
    session_start();

    if(isset($_GET['page'])) {
        $page = $_GET['page'];

        $_SESSION['curr_page'] = $page;

        if(isset($_GET['process'])) {

            $process = $_GET['process'];

            if(file_exists("pages/process_$process.php")) {
                include "pages/process_$process.php";
            } else {
                include "pages/404.php";
            }

        } else {
            if(file_exists("pages/body_$page.php")) {
                include "pages/body_$page.php";
            } else {
                include "pages/404.php";
            }
        }

    } else {
        include "pages/body_home.php";
    }
?>

Please help, thank you :)

Upvotes: 0

Views: 117

Answers (2)

Jonan
Jonan

Reputation: 2536

Use this:

RewriteRule ^([^/]+)/?$ main.php?page=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ main.php?page=$1&uid=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ main.php?page=$1&$2=$3 [L,QSA]
RewriteRule ^$ main.php [L,QSA]

Upvotes: 2

Reactgular
Reactgular

Reputation: 54731

Try changing

RewriteRule ^(.*)$ main.php?page=$1 [L,QSA]

To

RewriteRule ^(.*)$ main.php [L,QSA]

The query parameters will still be in $_GET

Upvotes: 2

Related Questions