【问题标题】:.htaccess rewrite with special url and IDs digits as parameters.htaccess 使用特殊 url 和 ID 数字作为参数重写
【发布时间】:2021-01-14 21:51:48
【问题描述】:

我建立了自己的 mvc 模型。

现在我想重写以下网址:

http://example.com/admin/index.php/Frontend/show?request[id]=24&request[lang]=33

http://example.com/24/33/

如何从根路径通过 .htaccess 重写该 url?

我试过了:

RewriteEngine on
RewriteBase /admin/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php/Frontend/show?request[id]=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php/Frontend/show?request[id]=$1&request[lang]=$2 [L]

我收到以下错误:

No input file specified.

谢谢!

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    问题:

    • 您可能只需要数字。
    • 你错过了最后一个/
    # Good
    RewriteRule ^([0-9]+)/([0-9]+)/$ index.php/Frontend/show?request[id]=$1&request[lang]=$2 [L]
    

    其次,这条规则太宽了。

    # Bad
    RewriteRule ^([^/]+)$ index.php/Frontend/show?request[id]=$1 [L]
    

    同样,您可能只需要数字和最后一个 /

    # Good
    RewriteRule ^([0-9]+)/$ index.php/Frontend/show?request[id]=$1 [L]
    

    最后,为了获得最佳实践,请为您永远不会使用的目录使用前缀。

    # Gooder
    RewriteRule ^show/([0-9]+)/([0-9]+)/$ index.php/Frontend/show?request[id]=$1&request[lang]=$2 [L]
    

    【讨论】:

    • 感谢您的回答。我尝试了您代码的最后一行,但仍然收到错误 No input file specified (fe if I type in URL example.com/show/24/33/) 是否有可能某些 apache 配置不允许我改写?也许重要的是要知道我的 index.php 文件在目录 /admin/ 下,所以我的 RewriteBase 是 /admin/。我的 .htaccess 在根目录中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多