自定义网址显示相同的页面

时间:2013-11-05 15:26:35

标签: php url mod-rewrite url-rewriting

我的应用程序为其用户创建自定义网址。例如:www.example.com/user1 直到这里一切正常。但是www.example.com/aboutwww.example.com/terms这样的自定义页面显示的内容与自定义网址相同,因为根据我的.htaccess文件,'example.com/'之后的任何内容都会显示'admin.php'文件的内容。我如何区分这两种情况。

2 个答案:

答案 0 :(得分:0)

您可以将www.example.com/user1更改为www.example.com/users/user1,也可以将www.example.com/about更改为www.example.com/pages/about

答案 1 :(得分:0)

我个人倾向于在url中命名对象,以便我可以根据对象的类型创建rewriterule,例如:

http://www.example.com/users/username
http://www.example.com/pages/about
http://www.example.com/news/xyz

如果这不是一个有效的解决方案,您将获得最佳服务,使用单个“门户”索引页并在PHP中解析正确的路由。所以你的重写就像:

RewriteEngine On

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

RewriteRule ^(.*)$ index.php [L]

然后在index.php中,您将使用更复杂的逻辑解析URL,例如:

<?php
$requested_url = explode('?', $_SERVER['REQUEST_URI']);
$requested_url = array_shift($requested_url); 
$requested_url_parts = explode('/', $requested_url);

if(!empty($requested_url_parts) && empty($requested_url_parts[0]))
     array_shift($requested_url_parts);

if(end($requested_url_parts) == "") 
     array_pop($requested_url_parts);

reset($requested_url_parts);

/* you now have an array of each part of the URL, so now you can load the valid page */
/* So to start maybe do a mysql lookup to see if current($requested_url_parts) is a valid username or not, if it is, load user page, if not, check if its a valid page e.c.t. */

?>