【问题标题】:Rewrite basic $_GET variable to friendly URL using mod_rewrite使用 mod_rewrite 将基本的 $_GET 变量重写为友好的 URL
【发布时间】:2012-03-01 04:51:37
【问题描述】:

我知道类似的问题很常见。我什至不知道从哪里开始用 /'s 和 .'s 重写规则我不知道如何将其他人的解决方案改造成我的。关于我的情况:

我在 index.php 文件上使用了一个基本的 get,看起来像

http://www.example.com/index.php?page=about

我想把它改写成

http://www.example.com/about

我知道这是相当简单的重写,但它只是一种完全不同的语言,我尝试过但未能理解。谢谢

【问题讨论】:

    标签: .htaccess mod-rewrite get


    【解决方案1】:

    试试这个:

    RewriteEngine on
    RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]
    

    【讨论】:

    • 所以这应该起作用的方式是,如果我将用户发送到 /about 服务器将自己重写为 /index.php?page=about .Correct?我问是因为我尝试了代码,但它对我不起作用。我已经在服务器的其他区域使用了 mod_rewrite,所以我知道与覆盖等无关。还进行了测试以确保整个 .htaccess 文件写入错误。出于某种原因,我无法让这个目录重写任何内容。
    【解决方案2】:

    甚至:

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME}  -f
    RewriteRule ^(?!index.php\b).*   index.php?page=$0 [L,QSA]
    

    注意:

    • 您应该始终在.htaccess 文件中设置RewriteBase
    • 我已经对此进行了概括,以便index.php 拾取 任何 未映射到真实文件的字符串
    • (?!index.php\b) 是一个正则表达式,表示“但不匹配 index.php
    • 如果您的请求可以包含请求参数,您将需要 [QSA] 标志。这将它们合并。

    【讨论】:

      猜你喜欢
      • 2013-10-29
      • 2015-02-14
      • 2012-06-07
      • 2023-03-11
      • 2019-05-29
      • 2011-01-04
      • 1970-01-01
      • 2012-06-29
      • 2012-09-25
      相关资源
      最近更新 更多