【问题标题】:how to convert url to friendly url in htaccess如何在htaccess中将url转换为友好的url
【发布时间】:2014-05-24 08:50:18
【问题描述】:
我有一个用于管理广告的 php 脚本,但我想使用友好的 url。
请给我所有这些示例的 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
【问题讨论】:
标签:
php
.htaccess
mod-rewrite
redirect
【解决方案1】:
你可以这样做:(它不会是动态的)
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
这是短期使用,必须最少。
【解决方案2】:
最好的方法是创建一个文件 index.php manaja 请求的 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');