【问题标题】:Mod_rewrite flat linksMod_rewrite 平面链接
【发布时间】:2011-04-14 08:08:19
【问题描述】:

我正在尝试将我的 get 变量显示为平面链接。 想从这里改变:

http://mydomain.com/index.php?page=shop&var1=hat&var2=10

http://mydomain.com/index.php/shop/hat/10

请记住,变量的数量不是固定的,这就是我使用 var1,var2,...等的原因

[编辑] 我已经有一个部分工作的脚本,但最多只能使用 3 个变量

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2&var2=$3 [L]
RewriteRule ^([^/\.]+)/?([^/\.]+)/?([^/\.]+)/?$ index.php?page=$1&var=$2&var2=$3&var3=$4 [L]

【问题讨论】:

    标签: php .htaccess mod-rewrite hyperlink flat


    【解决方案1】:

    所以首先要记住 mod_rewrite 是这样工作的:

    http://mydomain.com/index.php/shop/hat/10
    

    (客户端类型)被重写为

    http://mydomain.com/index.php?page=shop&var1=hat&var2=10
    

    (为客户服务的内容),但不一定像后者那样显示。 (除非您将其设为重定向)

    所以假设你的格式在这里被完整描述:

    RewriteRule ^index\.php/([^/]+)/([^/]+)/([0-9]+)$ /index.php?page=$1&var2=$2&var2=$3
    

    应该不错。

    编辑:

    哦,顺便说一句!我没有考虑变量的可变数量。我认为这不应该由 mod_rewrite 处理。也许最好的镜头是RewriteRule index.php/(.*) /index.php?call=$1,然后使用你的脚本使用/分隔符来爆炸。

    只有在您已经知道仅 AFAIK 的变量数时,您才能这样做。

    【讨论】:

    • 感谢您的帮助。您的脚本看起来不错,看起来应该可以工作,但它给了我“找不到对象”。也许我遗漏了一些东西,这是我在 RewriteRule 上实现的代码 RewriteEngine ^index\.php/([^/]+)/([^/]+)/([0-9 ]+)$ /index.php?page=$1&var2=$2&var2=$3 。该网站处于测试模式,并且不在域的根目录中
    • 啊!我的规则仅在您的 index.php 位于根目录时才有效。您是否尝试在 index.php 之前添加目录链接?
    • 这就像您在第二个解决方案中所期望的那样工作 ^(.*)?$ index.php?page=$1 [L] 。不适用于 index.php 或目录。这对我来说已经足够了。感谢您的帮助!
    【解决方案2】:

    您可以将所有请求直接指向 index.php(称为引导程序),然后让脚本解析出变量。

    Zend Framework 是这样处理的,你应该看看。 ZF 还有很多其他的好东西可供您使用。

    这是我的一个使用 ZF 支持的网站的虚拟主机的重写块。

        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI}      !^/favicon.ico
        RewriteRule ^.*$ index.php [NC,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 2013-11-11
      • 2013-02-08
      相关资源
      最近更新 更多