user5878353
user5878353

Reputation:

Get parameters in PHP after a htaccess rule

What would be the htaccess rule of the following case:

I redirect my user to this page:
system/abc123/step1

Without a rule it could be:
system.php?id=abc123&step=step1

But how can I get via $_GET the two parameters passed ?

Thanks a lot.

Upvotes: 2

Views: 38

Answers (2)

user5878353
user5878353

Reputation:

What I was looking for is :

In my .htaccess:

RewriteEngine on
RewriteRule ^system/(.+)/(.+)$  system?token=$1&step=$2 [L]

In my system.php page:

$QUO_Token = $_GET['token']; // abc123
$QUO_Step = $_GET['step'];   // step1

Upvotes: 1

fusion3k
fusion3k

Reputation: 11689

Your own solution is fine, and it works as expected, without touching php code.

However, for my scripts I adopt a different approach:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .  /processUrl.php [NC, L]

By this way, ALL (Not Foud) incoming requests all redirected to processUrl.php, in which there is a code like this:

$uri = (object) parse_url( $_SERVER['REQUEST_URI'] );
$uri->thread = explode( '/', substr($uri->path, 1) );

if( 'system' == $uri->thread[0] )
{
    $_GET['id']   = $uri->thread[1];
    $_GET['step'] = $uri->thread[2];
    include( 'system.php' );
}
elseif( 'browse' == $uri->thread[0] )
{
    // Other condition here
}
elseif( isset( $uri->query ) )
{
    // Process ORIGINAL $_GET
}
else
{
    include( 'Your404NotFoundPage.php' );
}

I prefer the above method due to:

  1. Transparency: I can see directly in code how different URI are treated;
  2. Flexibility: I can add condition or process new url without modify .htaccess file.

Upvotes: 0

Related Questions