您基本上可以通过两种方式做到这一点:
带有 mod_rewrite 的 .htaccess 路由
在根文件夹中添加一个名为 .htaccess 的文件,然后添加如下内容:
RewriteEngine on
RewriteRule ^/Some-text-goes-here/([0-9]+)$ /picture.php?id=$1
这将告诉 Apache 为该文件夹启用 mod_rewrite,如果它被询问与正则表达式匹配的 URL,它会在内部将其重写为您想要的内容,而最终用户不会看到它。简单但不灵活,因此如果您需要更多功能:
PHP 路线
Put the following in your .htaccess instead:
FallbackResource index.php
This will tell it to run your index.php for all files it cannot normally find in your site. In there you can then for example:
$path = ltrim($_SERVER['REQUEST_URI'], '/'); // Trim leading slash(es)
$elements = explode('/', $path); // Split path on slashes
if(count($elements) == 0) // No path elements means home
ShowHomepage();
else switch(array_shift($elements)) // Pop off first item and switch
{
case 'Some-text-goes-here':
ShowPicture($elements); // passes rest of parameters to internal function
break;
case 'more':
...
default:
header('HTTP/1.1 404 Not Found');
Show404Error();
}
This is how big sites and CMS-systems do it, because it allows far more flexibility in parsing URLs, config and database dependent URLs etc. For sporadic usage the hardcoded rewrite rules in .htaccess will do fine though.
*****Copy this content from URL rewriting with PHP**