Question

I am trying to get the ID of a URL after the last slash /. The URL looks like this:

mysite.com/ID

Right now, when i put ID after the slash, it gives me 404 Not found.

This code gets me the ID after slash, and full URL:


$url = $_SERVER['REQUEST_URI'];
echo "$url<br />";

$fullurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $fullurl;

But the problem is that it shows 404 Not found, when i put ID after slash. Is it something i have to change in my .htaccess?

Was it helpful?

Solution

You need to redirect your http://site.com/id URL to your .php file as

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*?)/?$ index.php?id=$1 [L]

The id now becomes available as $_GET["id"] in PHP. If you want to limit this to numbers use

RewriteRule ^(\d+)/?$ index.php?id=$1 [L]

If you want to pass /any/path/any/page.php to your PHP file use

RewriteRule ^(.*)$ index.php?uri=$1 [L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top