Insolenter
Insolenter

Reputation: 23

PHP $_GET .htaccess rewrite

htaccess:

RewriteEngine on
RewriteRule ^test/ test.php
RewriteRule ^test/success/ test.php?success

test.php:

<?php
if(isset($_GET['success']))
    echo "SUCCESS";
else
{
    echo "nothing is set";
}
?>

When I try to access test.php like /test.php?success it works and echos "SUCCESS". I want it to work the same way but overwritten in htaccess. I just want it to echo that message when I open it like /test/success/. If it is possible, is it correct?

Upvotes: 2

Views: 431

Answers (3)

Izzy
Izzy

Reputation: 412

RewriteEngine on

RewriteRule ^test/([a-zA-Z0-9-]*) test.php?subpage=$1    [NC]

And you can have links like: /test/put-here-any-text/

test.php
<?php
if(isset($_GET['subpage']) )
    echo $_GET['subpage'];
else
{
    echo "nothing is set";
}
?>

Upvotes: 0

Zartash Zulfiqar
Zartash Zulfiqar

Reputation: 103

RewriteEngine on

RewriteRule ^test/$ test.php

RewriteRule ^test/success/$ test.php?success

it will also work for you. If you don't need slash after success then you can remove it. And you have need to open test/success You must need to close .htaccess rewrite url on which you want to redirect your url.

Upvotes: 0

Ferrybig
Ferrybig

Reputation: 18844

.htaccess rewrite rules are read from the top to bottom,

If we apply this to your input string, we get

  • start with /test/success/
  • matches RewriteRule ^test/ test.php so it becomes test.php
  • test.php doesn't match RewriteRule ^test/success/ test.php?success

We end up with test.php.

However, this isn't what you want, because you want that the last url rewrites to test.php?success instead of the other file. There is a easy fix for this problem, we simple swap the 2 lines of rewrite rules so the longer one is listed first

Resulting .htaccess file:

RewriteEngine on

RewriteRule ^test/success/ test.php?success

RewriteRule ^test/ test.php

Upvotes: 2

Related Questions