Not returning the second parameter

Asked

Viewed 58 times

1

I am developing an api. Basically it requests a page according to the request. Example: http://localhost/mod/<name>/<version> the problem is being /version. I made htaccess that way:

Options +FollowSymLinks
RewriteEngine on

RewriteRule /mod/(.*) /mod.php?name=$1
RewriteRule /mod/(.*)/(.*) /mod.php?name=$1&version=$2
RewriteRule /modpack/(.*) /modpack.php?slug=$1
RewriteRule /modpack/(.*)/(.*) /modpack.php?slug=$1&build=$2

But if I require for example /mod/test/1.0 o no $_GET['version'] says index version is null.

<?php
    echo "It works! name=" . $_GET['name'] . ',version=' .$_GET['version'];
?>

I used this line: RewriteRule /mod/(.*)/(.*) /mod.php?name=$1&version=$2

But it puts the version together with the name:

Notice: Undefined index: version in C: xampp htdocs mod.php on line 2
It Works! name=test/1.0,version=

The same goes for /modpack/<slug>/<build>

RewriteRule /modpack/(.*)/(.*) /modpack.php?slug=$1&build=$2
  • yes ( just to complete the comment)

  • Must have to make a explode, the get doesn’t enter more (as far as I know) in this format... explode('/', URL) , then you have the segments and continue from there... I just don’t answer because I’m not 100% sure that this is the only/best way to do it

  • I even thought of using the explode but the problem is if any user put a / in the version ai will bugger the system.

  • You can remove the / end-time, $url = rtrim('/', URL) before making the explode

  • I’ll try.

1 answer

4


This is happening because. * is a very greedy regex, because it marries everything, including with the / bar, so you cannot separate the parameters.

The correct is to improve this regex. Example: ([^\/]+) (not to marry the /)

RewriteRule /modpack/([^\/]+)/([^\/]+) /modpack.php?slug=$1&build=$2 
  • Okay. I made the switch but the $_GET['version']is returning empty

  • http://prntscr.com/dg6je8

  • 1

    Rewriterule /modpack/([ /]+)/([ /]+) /modpack.php? Slug=$1&build=$2

  • It really did work. Thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.