mod_rewrite的,如何正确地改写链接 [英] mod_rewrite, how to rewrite links correctly

查看:232
本文介绍了mod_rewrite的,如何正确地改写链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重写我的网站的链接。 重写和我想要重写后看到之前可能联系:
/index.pl?mode=users&action=add - /用户/添加/
/index.pl?mode=streets&action=edit&id=7 - /街道/编辑/ 7 < BR> /index.pl?mode=users&action=info&id=7 - /用户/资讯/ 7 < BR> 等

I am trying to rewrite links on my site. Possible links before rewrite and what I want to see after rewrite:
/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
etc

的.htaccess的内容:

.htaccess content:

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] 

/用户/添加/ /街道/添加/ 均工作正常,但是...

问题:
/用户/编辑/ XX - 我不能接受perl脚本ID。为什么?
/用户/信息/ XX - 我甚至不能得到信息部分 /模式=用户和放大器;行动=信息和ID = 7 页(它必须显示与错误ID空白表

顺便说一句...我的网站有开关的结构。我的意思是,如果模式是用户,那么它加载users.pl,如果mode =街头装streets.pl等关于第二个问题 - 相信我有信息部分在users.pl!而链接 /模式=用户和放大器;行动=信息和放大器; n = 7 工作完美

/users/add/ or /street/add/ are working properly, but...

Problems:
/users/edit/xx - I can't accept ID in perl script. Why?
/users/info/xx - I can't even get to info section /?mode=users&action=info&id=7 page (it have to show blank table with wrong ID

Btw... My site has 'switch' structure. I mean if mode is users, then it load "users.pl", if mode=streets loading "streets.pl", etc. About 2nd problem - sure I have info section at users.pl! And link /?mode=users&action=info&id=7 work perfect.

PS:加入PHP的标签,因为它不是Perl的问题,但是PHP等于perl的,所以PHP的追随者能帮助我太多
p.p.s:SRY对我不是很好的英语。

p.s.: added php-tag because it is not 'perl' problem , but php is equal to perl, so php-followers can help me too
p.p.s.: sry for my not very good english.

推荐答案

您有太多的括号,使您的反向引用的off-by-之一:

You have too many parentheses, which makes your backreferences off-by-one:

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]

这两个规则必须围绕((编辑|删除|资讯))过多的括号((加))(虽然添加不会受到影响,因为$ 2正确后向引用的话)。

Those two rules had too many parentheses around the ((edit|delete|info)) and ((add)) (though the "add" isn't affected because $2 correctly backreferences it).

这篇关于mod_rewrite的,如何正确地改写链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆