2

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 ?

  • abc123

  • step1

Thanks a lot.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.