0

http://domain.com?keyword=value

...is easy to capture as its a simple GET request, however, I'd like to capture values that I pass directly after the / like so:

http://domain.com/value

And I can fetch those values like so:

preg_match("/[^\/]+$/", "http://www.domain.com/value", $matches);
$last_word = $matches[0]; // value

... however, the URL results in a 404 because its looking for a directory named value which is obviously non-existing.

eozzy
  • 66,048
  • 104
  • 272
  • 428

1 Answers1

3

A simple URL rewrite will do it. And you can remove that ugly regex.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA,NC]

Explanation:

RewriteEngine on - sets the rewrite engine on
RewriteBase / - / means the root of your project/website and is everything after the domain in this case
RewriteCond %{REQUEST_FILENAME} !-f - is not an actual file
RewriteCond %{REQUEST_FILENAME} !-d - is not an actual directory
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA,NC] - send everything after the domain to url key GET global variable

Now you can get the url with $_GET['url']. Of course, you can change the key to that GET.

machineaddict
  • 3,216
  • 8
  • 37
  • 61