【问题标题】:PHP .htaccess -> pretty url (in reverse)PHP .htaccess -> 漂亮的 url(反向)
【发布时间】:2011-07-31 05:13:01
【问题描述】:

我知道如何进行 URL 的重写,例如: www.example.com/index.php?id=1&cat=3www.example.com/1/3/(或其他)。我知道。

我不知道到底如何将所有页面中的整个链接更改为链接到漂亮的 URL。我网站的所有链接都是老式的 (<a href="index.php?id=1&cat=2">),而且有很多。

我在问如果用户点击 index.php?id=1,是否有人有想法或知道如何自动重定向到那个漂亮的 url。 (如果您更改 url 中的标题,几乎就像这个站点 Stackoverflow)。

所以我的推测是……

  1. 使用 .htaccess 读取 index.php?id=1&cat=2 来重写 index/1/3 自己再次解释(奇怪)

  2. 一个 php 文件来执行 htaccess 重写回原始的重定向...

结论:将<a href="index.php?id=1&.....">自动改为index/1/2


已解决

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

##################################
# This turns index.php?id=1&cat=2 into index/1/2 and then back 'transparent' into    index.php?id=1&cat=2 if you have old fashioned
# links in your site and don't want to change them :)


# Avoid mod_rewrite infinite loops 
# This is critical if you want to use this code

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

# Hard-rewrite ("[R]") to "friendly" URL.
# Needs RewriteCond to match original querystring.
# Uses "?" in target to remove original querystring,
#   and "%n" backrefs to move its components.
# Target must be a full path as it's a hard-rewrite.
RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$
RewriteRule ^index.php$ http://localhost/index/%1/%2/? [L,R]

# Soft-rewrite from "friendly" URL to "real" URL.
# Transparent to browser.
# Won't re-trigger the above rewrite, though I'm
#   not really sure why! The order of the rules
#   doesn't seem to make a difference.
RewriteRule ^index/(\d+)/(\d+)/$ index.php?id=$1&cat=$2 [L]

【问题讨论】:

    标签: php mod-rewrite rewrite friendly-url


    【解决方案1】:
    RewriteEngine on
    
    # Prevents browser looping, which does seem
    #   to occur in some specific scenarios. Can't
    #   explain the mechanics of this problem in
    #   detail, but there we go.
    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule .* - [L]
    
    # Hard-rewrite ("[R]") to "friendly" URL.
    # Needs RewriteCond to match original querystring.
    # Uses "?" in target to remove original querystring,
    #   and "%n" backrefs to move its components.
    # Target must be a full path as it's a hard-rewrite.
    RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$
    RewriteRule ^index\.php$ http://example.com/index/%1/%2/? [L,R]
    
    # Soft-rewrite from "friendly" URL to "real" URL.
    # Transparent to browser.
    RewriteRule ^index/(\d+)/(\d+)/$ /index.php?id=$1&cat=$2
    

    当然,理想情况下,您只需修复链接,然后只需要软重写。 :)

    用 Apache/2.2.3 测试。我想我编造了“硬重写”和“软重写”这两个术语。

    【讨论】:

    • 如果它会导致无限循环,这就是技巧,我猜它会,因为在它的底部会有RewriteRule ^(index)/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/?(.*)$ index.php?param1=$2&param2=$4 [L,QSA,NC] RewriteRule ^(index)/([a-z0-9]+)(/[^/]+)?/?(.*)$ index.php?param1=$2 [L,QSA,NC]
    • @pufos:老实说,我不确定它是否会。这两个规则有不同的标志可能阻止循环(因为第一个使浏览器执行显式重新请求,而第二个只是默默地重写目标位置)。但这需要检查。我公开愿意接受这可能行不通。
    • result: not working ... if i try to access the file from index.php?id=1&... it does'n rewrite it to /index/1/3 it leave it like that index.php?id=....
    • 这真是令人兴奋,我无法从逻辑上理解它.. :(
    • Chrome 说“重定向循环”:|
    【解决方案2】:

    为什么不直接改变你的 index.php 文件呢?理论上,您可以通过这种方式进行更多的错误检查,允许变量按任何顺序排列,并且仍然可以路由到正确的结束位置。

    <?php
        // Permanent redirection
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: http://www.example.com/{$_GET['id']}/{$_GET['cat']}");
    

    我这里没有做任何错误检查,但想给出一个基本的例子。

    再想一想,我猜这是在 index.php 文件中添加功能,然后您想将其用于您的应用程序本身,因此可能最终会混淆代码中的功能。

    【讨论】:

    • 你说...在 index.php 中做匹配模式?或者我应该将所有页面重定向到单个页面并匹配 patterns there. But i don;t know how because i have index.php , edit.php, insert.php, run.php and every of those php file are generating link old fashion` 像 &lt;a href=index.php?id.....&gt; 以及像 tu category.php?cat=12 ..
    • 好吧,为了让“漂亮”的 url 起作用,你必须为它们设置一个重定向。从理论上讲,您可以将该重定向结果作为 application.php (我只是随机选择了一个名称),这将允许这两个部分仍然单独存在但具有您的功能。您实际上不必在 index.php 中进行任何匹配,因为这些值会神奇地在 PHP 超全局变量中可用(这就是 $_GET 之一)。因此,您可以从该数组中引用它们,例如$_GET['id'] 会给你 '1' 你的例子。
    • 啊,好吧,那么是的,如果 URL 很多,那就有点复杂了。您可能必须明确地将它们排除在外,因为您的新漂亮 URL 和它们的处理方式之间可能会发生一些冲突。然后,您可以将所有 (.*)\.php(.*) URL 重定向到一个 redirector.php 文件,然后该文件将重定向到最终结果,但 2 阶段重定向并不是最好的。所以你留下了一个更复杂的正则表达式,我将不得不考虑它。
    • 是的,因为我想维护 &lt;a href=index.php?id...&gt; 并在单击链接到 http://example.com/index/1/3/ 时让 .htaccess 解释它并将其重写为 index.php?id=$1 :( ....如何计算这出来了?
    猜你喜欢
    • 2012-05-07
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2019-02-27
    相关资源
    最近更新 更多