0

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 :)

2 Answers 2

2

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

5 Comments

@MathewFoscarini I also changed the *s into +s
These rules definitely need RewriteCond %{REQUEST_FILENAME} !-d and RewriteCond %{REQUEST_FILENAME} !-f conditions.
@anubhava I don't think so. In the question there already is RewriteRule ^[css|images|js|config|lib|pages] - [L,NC] which suggests that the OP doesn't want any other files than in those directories to be directly accessible
All I can humbly suggest is to test your rules. You will be surprised to see results yourself.
@fanjavaid please hit me over the head. I forgot to put capturing brackets () around [^/]+. See my edited answer, I tested it and it works now
2

Try changing

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

To

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

The query parameters will still be in $_GET

4 Comments

@fanjavaid well, my answer will still work. main.php will get executed for all requests that are not css|images|js..... You then have to look write PHP code to handle the routing yourself.
@MathewFoscarini no it won't. If you go to localhost/websekolah/register, the page parameter won't be set.
@Jonan ah you are correct. I do routing in PHP and send everything to a main pHP file. From there I have a routing table.
I already try your answer but it access homepage, when i access http://localhost/websekolah/register

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.