douhuxi4145 2011-07-20 20:59
浏览 52
已采纳

使用.htaccess隐藏URL密钥参数

I've been searching all over the web and haven't yet found any solution to this issue. I'm hoping you could shed some light on the situation.

I have my index file set up like this:

<header></header>
<div id="main">
    <?php
    if(isset($_GET["p"])) $p = $_GET["p"];
    else $p = "home";

    if(file_exists("pages/{$p}.php")) include("pages/{$p}.php");
    ?>
</div>

which makes me load my pages with a ?p=contact href.

Say I would like to display a users profile. I'd then create a subfolder in my "pages" folder, making the relative path pages/users/profile.php, thus the href ?p=users/profile&uid=5. But that leaves an ugly URL (as well as SEO rating).

How would I rewrite that URL to look like /users/profile/5?


EDIT: I've tried the following, resulting in HTTP 500:

RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&uid=$2 [L]

EDIT: My .htaccess file, located directly inside root folder: http://pastie.org/2268239
Line 338 is where I want to achieve this (currently just a comment).

  • 写回答

3条回答 默认 最新

  • duanruoyu6675 2013-04-20 09:43
    关注

    I achieved the desired effect by adding these three lines:

    RewriteRule ^([a-zA-Z]+)$ index.php?p=$1 [L]
    RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)$ index.php?p=$1/$2 [L]
    RewriteRule ^([a-zA-Z]+)/([0-9]+)$ index.php?p=$1&uid=$2 [L]
    

    This allows me to access /contact, /users/index and /users/profile/5.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度