0

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

4
  • 1
    Out of curiosity, how can one study programming/scripting material for a month without writing a line of code? Commented Oct 20, 2014 at 19:52
  • My point is to do this 2var.php/one/two what you wrote is for /one/two.html Commented Oct 20, 2014 at 19:53
  • 1
    @rr- because I have a bit a busy life(not the cocky way, just much to do), and I learned with an app on my phone while taking buss/metro. I can PHP/CSS/HTML/JS/JQUERY, but I want to learn something more so I take every moment I can. Off-topic btw Commented Oct 20, 2014 at 19:54
  • If u are rewriting then u make new links. The rewriting does not change the URLs for U, they are just linked internal to the right script. In other words ur href should be one/two.html. Don't create the href with the $_GET's Commented Oct 20, 2014 at 19:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So it seems that I missunderstod a bit the use of ^ $. So in general we can say that it was a bit dumb of a question, like you said, var2.php is already a script

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.