Blur
Blur

Reputation: 1286

How do I $_GET correct ID

picture.php

if (!isset($_GET['id'])) {
    header('location:/');
    exit();
} else if (isset($_GET['id'])) {
    print_r($_GET);
}

$_GET output

 Array ( [page] => picture ) 

.htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/picture/?$ /picture/?id=$1 [L]

URL format : http://local.com/42/picture/

Question : How to make the $_GET['id'] will print 42 and not the picture

Upvotes: 0

Views: 72

Answers (2)

Gumbo
Gumbo

Reputation: 655499

The L flag causes a re-injection of the already rewritten URL into the rewriting process. So when /42/picture/ gets rewritten to /picture/?id=42 it then gets rewritten to /index.php?page=picture.

You can avoid this by putting a rule in front of your other rules that stops the rewriting process for every request that’s path can be mapped onto an existing file:

RewriteCond %{REQUEST_FILNAME} -f
RewriteRule ^ - [L]

Upvotes: 1

RageZ
RageZ

Reputation: 27323

From the output of your var_dump, it seems the first rule is matching and not the second. The order you write rules matters, you should probably revert the two rules. Since your first rule is shorter.

RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-]+)/picture/?$ /picture/?id=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?page=$1 [L]

You can take a look at the documentation for your reference.

Upvotes: 1

Related Questions