有不同的方法可以达到相同的结果,但这将为您提供指导。
首先激活 mod_rewrite 取消注释 httpd.conf 或 apache2.conf 文件中的这一行:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
将 AllowOverride 从 none 更改为 All 到您的 www 目录
<Directory />
AllowOverride All
Require all denied
</Directory>
重启apache:
sudo apachectl restart
或
sudo service apache2 restart
在您的 www 文件夹中,创建一个 .htaccess 文件并添加:
# | SEO URL
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
然后在你的 index.php 文件中:
<?php
$url_params=get_url_params();
// then access your url_params
if (isset($url_params[1]))
{
switch ($url_params[1])
{
case 'login':
echo "<h1>Login</h2>";
break;
case 'contact':
echo "<h1>Cantact</h2>";
break;
default:
echo "<h1>Home</h2>";
break;
}
}
function get_url_params($site_url='')
{
$base_url=explode("/", $site_url);
$request = $_SERVER['REQUEST_URI'];
$url_params = explode("/", $request);
$delete_extensions=array('.html','.htm');
$data[]=array();
foreach ($base_url as $b)
{
unset( $url_params[array_search( "$b", $url_params )] );
}
foreach ($url_params as $u)
{
foreach ($delete_extensions as $e){
$u=str_replace($e, "", $u);
}
$data[]=$u;
}
return $data;
}
?>