【问题标题】:mod_rewrite, how to rewrite links correctlymod_rewrite,如何正确重写链接
【发布时间】:2014-02-01 22:44:35
【问题描述】:

我正在尝试重写我网站上的链接。 重写前可能的链接以及重写后我想看到的链接:
/index.pl?mode=users&action=add - /users/add/
/index.pl?mode=streets&action=edit&id=7 - /streets/edit/7
/index.pl?mode=users&action=info&id=7 - /users/info/7
等等

.htaccess 内容:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/((edit|delete|info))/([0-9]+)$ index.pl?mode=$1&action=$2&id=$3 [L]
RewriteRule ^([A-Za-z0-9-]+)/((add))/?$ index.pl?mode=$1&action=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.pl?mode=$1 [L] 

/users/add//street/add/ 工作正常,但是...

问题:
/users/edit/xx - 我不能在 perl 脚本中接受 ID。为什么?
/users/info/xx - 我什至无法进入信息部分 /?mode=users&action=info&id=7 页面(它必须显示 ID 错误的空白表

顺便说一句...我的网站有“开关”结构。我的意思是如果模式是用户,那么它会加载“users.pl”,如果模式=streets 加载“streets.pl”等。关于第二个问题 - 确定我在 users.pl 有信息部分!并链接/?mode=users&action=info&id=7 完美运行。

p.s.: 添加 php-tag 因为它不是 'perl' 问题,但是 php 等于 perl,所以 php-followers 也可以帮助我
p.p.s.:对不起,我的英语不太好。

【问题讨论】:

    标签: php perl apache .htaccess mod-rewrite


    【解决方案1】:

    你有太多的括号,这会使你的反向引用减少一个:

    RewriteRule ^([A-Za-z0-9-]+)/(edit|delete|info)/([0-9]+)$ index.pl?mode=$1&action=$2&id=$3 [L]
    RewriteRule ^([A-Za-z0-9-]+)/(add)/?$ index.pl?mode=$1&action=$2 [L]
    

    这两条规则在 ((edit|delete|info))((add)) 周围有太多括号(尽管“添加”不受影响,因为 $2 正确地反向引用了它)。

    【讨论】:

    • 是的。你说的对。完全忘记了。还有1个问题:我只有users.pl脚本中的信息部分,街道和其他人没有。是否可以仅与用户访问信息:/users/info/7 并对其他人进行限制:/streets/info/7 - 不能工作。无论如何,也许您可​​以以某种方式改善这些表达方式? @乔恩林
    • @SharikovVladislav 您将需要专门为^users/info/([0-9]+)$ 模式创建一个单独的规则,并从更一般的规则中删除“信息”。
    • 看,如果我输入不正确的链接(不符合规则)我会得到 403,对吧?是否可以将浏览器重定向到主页?或者最好创建 403 错误页面并将浏览器重定向到该页面? @乔恩林
    • @SharikovVladislav 由你决定,如果你想重定向到主页,你需要将F 更改为R。但老实说,只返回 404 Not Found 有什么问题?这就是响应代码的目的,URL 不存在。
    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2011-06-07
    • 2012-11-14
    • 1970-01-01
    • 2022-03-11
    相关资源
    最近更新 更多