【问题标题】:Apache mod_rewrite: what am I doing wrong?Apache mod_rewrite:我做错了什么?
【发布时间】:2011-09-23 02:34:32
【问题描述】:

我在我的 .htaccess 文件中进行了非常简单的重写,但它并不能完全按照我想要的方式工作。这是我的代码:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^admin/?$ admin.php [L]
    RewriteRule ^([0-9]*).*$ index.php?id=$1 [L]
</IfModule>

基本上我想要的是每个页面都是这样的:/0/title。标题只是为了让用户的 URL 更清晰,但数字(id)应该传递给我的 PHP 脚本。使用此代码,id 不会传递给我的 index.php 脚本。如果我只是从第四行中删除“.*”,它将被传递给该脚本,但是数字后带有文本的 URL 不会传递给我的 index.php 文件。

我做错了什么?我该如何解决这个问题?

谢谢!

【问题讨论】:

  • 您想将您的 index.php 称为 index.php?id=0 还是像 index.php?id=0&amp;t=title 这样的其他名称?

标签: php apache mod-rewrite friendly-url


【解决方案1】:

您正试图捕获像 /0/title 这样的 URL,但您的匹配模式中没有斜线 /。试试这个:

# Should match /01234/anything
# with the "/anything" optional
RewriteRule ^([0-9]+)(/.*)?$ index.php?id=$1 [L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 2021-04-14
    • 2021-04-19
    • 2017-03-13
    相关资源
    最近更新 更多