我正在尝试使用 php 制作 SEO 友好的网站...
在文件夹 jcheck 中,我有一个 index.php 文件,它以一个参数“id”作为输入。
mydomain.com/jcheck/index.php?id=1 works
我怎样才能像下面这样
mydomain.com/jcheck/1
我试过制作 .htaccess 文件并放入
RewriteEngine on
RewriteCond %{REQUEST_URI} 1/
RewriteRule 1/ http://mydomain.com/jcheck/index.php?id=1
我怎样才能让它发挥作用?
您基本上可以通过两种方式执行此操作:
带有 mod_rewrite 的 .htaccess 路由
在您的根文件夹中添加一个名为 .htaccess 的文件,并添加如下内容:
RewriteEngine on
RewriteRule ^/Some-text-goes-here/([0-9]+)$ /picture.php?id=$1
这将告诉 Apache 为该文件夹启用 mod_rewrite,如果它被询问一个匹配正则表达式的 URL,它会在内部将其重写为您想要的内容,而最终用户不会看到它。简单但不灵活,因此如果您需要更多功能:
PHP 路由
改为将以下内容放入您的 .htaccess 中:
FallbackResource index.php
这将告诉它为它通常无法在您的站点中找到的所有文件运行您的 index.php。例如,您可以在那里:
$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();
}
大型站点和 CMS 系统就是这样做的,因为它在解析 URL、配置和数据库相关 URL 等方面提供了更大的灵 active 。对于零星的使用,.htaccess 中的硬编码重写规则会很好。
*****从URL rewriting with PHP复制此内容**