calexandru
calexandru

Reputation: 307

htaccess + regex; sending variables + url rewrite

I'm kinda training my htacces+regex skills, been studying in theory for like a month or two, and wanted to give it a shot in practice.

So the thing is I have a file called index.php with the following code:

<html>
    <head>
    </head>
    <body>
        <a href="2var.php?var1=one&var2=two">2var test</a><!--2var.php/one/two-->
    </body>
</html>

and a second file called 2var.php that has the following code:

<?php

if($_GET["var1"] && $_GET["var2"])
{
    echo "both rechead";
}
else
{
    if($_GET["var1"] || $_GET["var2"])
        echo "one of them reached";
    else
        echo "neither reached";
}
?>

So, what I wanted to do is to test if I can send variables and edit the url at the same time(I've been reading that is possible and that's exactly what's happening "behind the curtains"). But I can't get the right mind-set or the knowhow to accomplish this.

Btw, htacces:

DirectoryIndex index.html index.php
Options +FollowSymLinks
RewriteEngine On

RewriteRule ^\/?([a-z]+)\/([a-z]+).html$ 2var.php?var1=$1&var2=$2

My point is to do this 2var.php/one/two and that the variables are actually passed. Now I recieve only "neither reached";

EDIT: I'm in localhost so the hole link is localhost/tests/index.php || localhost/tests/var2.php?var1=$1&var2=$2

Upvotes: 0

Views: 73

Answers (1)

rr-
rr-

Reputation: 14811

You said you want to catch 2var.php/one/two.

The thing is that 2var.php is already a PHP script, and it so happens that you don't actually need RewriteEngine for this. If you try to visit your script with RewriteEngine and its rules disabled, it will work, but produce warning about undefined index: var1.

That is because the /one/two part isn't accessible in $_GET. Instead, it's stored in variable called $_SERVER['PATH_INFO'].

An example - visiting a URL like /test/2var.php/one/two, while having 2var.php contain following:

<?php
var_dump($_SERVER['PATH_INFO']);

will output following:

string(8) "/one/two" 

You can parse this with regular expressions in the script itself.


Regarding actual .htaccess rules. You almost got them right. However, you started with ^, which causes the URL to match strings exactly in form of /one/two.html. Since these matches happen against full request URI, go ahead and try see what happens if you replace ^ with 2var.php and remove the .html part so that your RewriteRule looks like this:

RewriteRule 2var.php\/?([a-z]+)\/([a-z]+)$ 2var.php?var1=$1&var2=$2

Note lack of ^. This means that you can put 2var in any directory on the server and it will still work.


Also, I noticed you link to <a href="2var.php?var1=one&var2=two">. The point of having rewrite rules is to make URLs pretty, so you can directly link to <a href="2var.php/one/two"> instead (or any other syntax you desire). As long as you back them up with proper rewrite rules, rewrite engine will take care of requests arriving at these addresses and will dispatch such requests to your final scripts.

Upvotes: 1

Related Questions