【问题标题】:.htaccess url rewrite querystring.htaccess url 重写查询字符串
【发布时间】:2013-02-12 07:35:36
【问题描述】:

好的,我已经阅读并在这里搜索了一些内容,但还没有找到我的问题的答案,或者我对 url 重写太陌生,只是想不通。这是我想要完成的任务:

我有一个包含频道描述和 ID 的表,ID 用于知道要显示的内容,所以我的 URL 中有这样的内容

http://www.mysite.com/page?channel=1

现在我想做的是显示这个:

http://www.mysite.com/page/description

并且仍然能够以某种方式获取该描述的 id 以显示适当的内容。希望这是有道理的。我唯一想到的是在我的页面顶部,执行:

select * from channels where description = $_GET['description']

并让它返回 id,然后使用它。那会是唯一的出路吗?还是 .htaccess 足够好?这样的菜鸟:(

编辑:这现在在我的 htaccess 中:

AddType x-mapp-php5 .php
Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
##This is to redirect just stream page only
RewriteRule ^stream/(.+)/([0-9]+)$ /stream.php?channel=$1

【问题讨论】:

  • 看我的回答here

标签: php .htaccess url-rewriting


【解决方案1】:

如果你想得到 ID,你 shell 下一步:

  1. 启用 mod_rewrite apache 模块
  2. 在 DocumentRoot 目录中创建 .htaccess
  3. 粘贴下一个:

    RewriteEngine On
    RewriteRule ^([0-9]+)$ /page.php?channel=$1
    
  4. 当您关注 URL 时:

http://www.mysite.com/1

您从 URL 中获取内容:

http://www.mysite.com/page?channel=1

【讨论】:

  • 这会起作用,除了我不想显示实际的“ID”值“1”而是它的描述,这可能吗?还是 URL 必须看起来像 mysite.com/page/description/1
  • ^([0-9]+)$ /page.php?channel=$1 更改为 ^page/description/([0-9]+)$ /page.php?channel=$1
  • "description" 也是动态的,所以会是 "^page/$1/([0-9]+)$ /page.php?channel=$2" 吗?
  • ^page/(.+)/([0-9]+)$ /page.php?channel=$2
  • 看看我的编辑,我添加了我当前在我的 htaccess 文件中的内容,当添加你的代码时它会中断并且我得到一个找不到页面的错误
【解决方案2】:

有很多方法可以做到这一点。

在您的应用程序中使用MVC 架构将迫使您在formanother 中使用路由逻辑。

您特别想要的可以通过多种方式完成,包括 apache RewriteMap

我会亲自设计我的应用程序,使其接收 URI 并将它们路由到控制器/页面。这可确保 PHP 继续完全控制请求最终显示的内容。

假设您的任务是在不影响大部分应用程序的情况下美化您的链接,您需要找到将/page/description 映射到/page?channel=1 的方法。

对于这种独特的情况,我会使用类似于decorator pattern 的东西,因为本质上你的任务不需要修改现有的代码库:

.htaccess

RewriteEngine On
RewriteRule ^.*$ router.php [NC,L]

router.php

include'config.php';// remove your config loading from index.php

$request = $_SERVER['REQUEST_URI'];
$request = explode('/', $_request);
    # in $request you will have all the uri segments now

/* complex logic to find out what page you're on */

   # assuming we found out it's page/description
$_GET['channel'] = 1;// or getIdFromDescription($request[2])
   # this forces the environment to believe it got a request with ?channel=1

# revert to your old request with injected environment
include'index.php';

这里的前提是 index.php 可以加载你的旧页面。你只需要改变它们的访问方式。因此,您使用路由器将新请求映射到旧请求。

它并不完美,但在设计不佳的应用程序上效果很好。

注意:我当前的实现不处理静态资源(图像、css、js 等)

【讨论】:

  • 我明白了大部分内容,我在 router.php 中的问题是我看到你正在硬编码 $_GET['channel'] = 1,但是如果它的页面/不同描述等于到 channel=3,知道我的意思吗?
  • // or getIdFromDescription($request[2]) 我无法为您实现该功能,请阅读 cmets 以了解您需要做什么才能使其完全正常运行。
  • 哦,对不起,出于某种愚蠢的原因,我像 javascript 代码 getid 一样阅读它......对不起
  • 稍微更新了我的 cmets,将带有 # 的任何评论视为解释逻辑的实际评论,以及所有其他 cmets 指导您如何使其工作:)
猜你喜欢
  • 2012-11-15
  • 2023-03-04
  • 2012-06-16
  • 2012-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-16
相关资源
最近更新 更多