【问题标题】:Custom url display same page自定义url显示同一页面
【发布时间】:2013-11-16 12:49:43
【问题描述】:

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

【问题讨论】:

  • "根据我的 .htaccess 文件,'example.com/' 之后的任何内容都将显示 'admin.php' 的内容"。这似乎是一个非常非常糟糕的主意,您需要在每个页面上解决,也许您应该考虑替代 .htaccess 规则。

标签: php url mod-rewrite url-rewriting


【解决方案1】:

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

【讨论】:

  • 如果您查看 facebook 的个人资料网址。他们是如何管理的?
【解决方案2】:

我个人倾向于在 url 中命名对象,这样我就可以根据对象的类型创建重写规则,例如:

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. */

?>

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 2019-09-14
    • 2013-06-16
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2022-07-27
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多