Sam
Sam

Reputation: 2565

How do I remove "?" in query string with mod_rewrite in htaccess?

I'm struggling with (what I think should be) a fairly simple mod_rewrite task. Essentially, I need to remove the "?" in all query strings. I should note, it's a weird php app where there are only names (no values) for the query string. So, I need this:

http://mysite.com/?cheese-is-tasty

to become:

http://mysite.com/cheese-is-tasty

Just to be clear, I want the user to type in the url without the "?".

My attempts and Googling around lead me to the following .htaccess file additions:

RewriteEngine on
RewriteRule (.*) \?$1

Am I anywhere close to the right solution?

Upvotes: 0

Views: 327

Answers (2)

iceduck
iceduck

Reputation: 163

Here's the .htaccess part :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L,QSA]

And then you have to parse the url with PHP:

<?php
    $requestURI = explode("/", $_SERVER["REQUEST_URI"]);
    $scriptName = explode("/",$_SERVER["SCRIPT_NAME"]);

    for($i= 0;$i < sizeof($scriptName);$i++)
            {
          if ($requestURI[$i] == $scriptName[$i])
                {
                    unset($requestURI[$i]);
                }
          }         
    $route = array_values($requestURI);

  /* for http://www.domain.com/page/view/1 the variable will be :
    Array ( [0] => page [1] => view [2] => 1) */

    $page = $route[0];


?>

And with $page you can load whatever the page you need to load. More detailed source : http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/

Upvotes: 1

Menztrual
Menztrual

Reputation: 41607

I use the following myself:

RewriteRule ^(.*)$ index.php?page=$1 [L]

And then in index.php, something like:

<?php
$page = $_GET['page'];
...
?>

Upvotes: 1

Related Questions