【问题标题】:Force a prettified URL with mod_rewrite使用 mod_rewrite 强制美化 URL
【发布时间】:2009-06-26 02:04:31
【问题描述】:

所以已经将我丑陋的 php URL 改写成更漂亮的东西:

RewriteEngine On
RewriteRule ^main/photo([^/\.]+)/?.html$ main/details.php?gid=$2pid=$1

但是,现在我想强迫任何人去

http://www.example.com/main/details.php?gid=20&pid=5

重定向到

htto://www.example.com/main/photo5.html

我已经尝试了以下 RewriteCond:

RewriteCond %{REQUEST_URI} ^/main/details\.php [NC]
RewriteRule ^main/details.php?gid=(.*)&pid=(.*) http://www.example.com/main/photo$1.html [R=301,L]

但这没有用。有什么想法吗?

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    您需要查看request line 以确定当前请求URI 是否是原始的。此外,您需要使用RewriteCond 指令来测试URI 的查询:

    RewriteCond %{THE_REQUEST} ^GET\ /main/details\.php
    RewriteCond %{QUERY_STRING} ^(([^&]*&)*)pid=([0-9]+)(&.*)?$
    RewriteRule ^main/details\.php$ /main/photo%3.html? [L,R=301]
    

    【讨论】:

    • 我被发送到 photo.html?pid=5.
    • 现在我被重定向到 photogid=73&.html。哪个部分决定了 %1 捕获的内容?
    • 编辑:找到了,您需要将 %1 更改为 %3。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2011-05-22
    • 2010-11-22
    相关资源
    最近更新 更多