如何在htaccess中将网址转换为友好网址

时间:2014-05-24 08:50:18

标签: php .htaccess mod-rewrite redirect

我有一个管理广告的PHP脚本,但我想使用友好的网址。

请为我提供所有这些示例的htaccess代码:

mydomain.com/index.php?page/login
to
mydomain.com/login


mydomain.com/index.php?page=register&step=2
to
mydomain.com/register/step-2/


mydomain.com/index.php?page=articles&category=countries&article=afghanistan
to
mydomain.com/articles/countries/afghanistan

2 个答案:

答案 0 :(得分:0)

你可以:(它不会是动态的)

Options +FollowSymLinks

RewriteEngine On
RewriteRule /login/(.*)$ /index.php?page/login
RewriteRule /register/step-2/(.*)$ /index.php?page=register&step=2
RewriteRule /articles/countries/afghanistan/(.*)$ /index.php?page=articles&category=countries&article=afghanistan

这是短暂使用,必须极少。

答案 1 :(得分:0)

最好的方法是创建一个文件index.php manaja request urls

// .htaccess
Options -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

和index.php

$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);

示例如果用户调用url // localhost / login

// you include the file if exists
include('rout to file/'.$url[0].'.php');