0

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?

6
  • As always, "thanks" are redundant. Avoid this meaningless boilerplate and, instead, just contribute back! Commented Mar 14, 2012 at 2:41
  • Politeness is redundant. Got it. Thanks... whoops! Commented Mar 14, 2012 at 3:22
  • 1
    No, politeness is expected, but brevity saves time for those reading Qs. The time to say thank-you is when you get a good point or a solution. When you +1 or tick a comment or ans; this also give a tangle reward in terms of reputation :-) Commented Mar 14, 2012 at 8:23
  • It's not actually any more polite to write the signature than it is to leave it out. It's just boilerplate and thus has no meaning. Commented Mar 14, 2012 at 9:59
  • Umm... @LightnessRacesinOrbit, did you really go and remove the "thanks" from all of my old questions? Really?! Even my questions from way back in 2009? Well, if you feel that strongly about it (and have the time to spare), I guess that's fine. One quick question: is this a Stack Overflow rule, or more of a personal pet peeve? I'm not trying to pick a fight here, I'm genuinely curious. Commented Mar 15, 2012 at 2:01

2 Answers 2

1

I use the following myself:

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

And then in index.php, something like:

<?php
$page = $_GET['page'];
...
?>
Sign up to request clarification or add additional context in comments.

2 Comments

It transfers the parameters just ok, but shouldn't you make a rewritecond to check that it won't loop forever to the 'index.php' request?
Not exactly what I was looking for, but it lead me to my solution. Thanks!
1

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/

Comments

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.