htaccess 用第二个参数重写 [英] htaccess rewrite with 2nd parameter

查看:51
本文介绍了htaccess 用第二个参数重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用此规则重定向到个人资料页面,效果很好:

Currently I'm using this rule to redirect to profile page, which works fine:

RewriteRule ^profile/([^/]*)/ ./profile.php?do=view&profileid=$1 [L,NC]

问题是我需要传递第二个参数,所以urlprofile/1/Chris/2 指向profile.php?do=view&profileid=$1&tab=2

The problem is that I need to pass a second parameter, so the url profile/1/Chris/2 to point to profile.php?do=view&profileid=$1&tab=2

有什么简单的方法吗?

谢谢

推荐答案

这当然是可能的:

RewriteEngine on
RewriteRule ^/?profile/(\d+)/[^/]+/(\d+)/?$ /profile.php?do=view&profileid=$1&tab=$2 [END]
RewriteRule ^/?profile/(\d+)/? /profile.php?do=view&profileid=$1 [END]

如果您使用上述规则收到内部服务器错误(http 状态 500),那么您可能运行的是非常旧版本的 apache http 服务器.在这种情况下,您将在 http 服务器错误日志文件中看到不支持的 [END] 标志的明确提示.您可以尝试升级或使用旧的 [L] 标志,在这种情况下它可能会起作用,尽管这有点取决于您的设置.

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

此规则在 http 服务器主机配置或动态配置文件(.htaccess"文件)中同样适用.显然重写模块需要在http服务器中加载并在http主机中启用.如果您使用动态配置文件,您需要注意在主机配置中完全启用它的解释,并且它位于主机的 DOCUMENT_ROOT 文件夹中.

This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

还有一个一般性的评论:你应该总是更喜欢在 http 服务器主机配置中放置这样的规则,而不是使用动态配置文件(.htaccess").这些动态配置文件增加了复杂性,通常是意外行为的原因,难以调试,而且它们确实会降低 http 服务器的速度.它们仅在您无法访问真正的 http 服务器主机配置(阅读:非常便宜的服务提供商)或坚持编写自己的规则的应用程序(这是一个明显的安全噩梦)的情况下作为最后一个选项提供.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

更新:考虑到您关于如何处理第二个参数的附加评论,这可能实现了该特定要求:

UPDATE: considering your additional comment about how that second parameter is to be treated this probably implements that specific requirement:

RewriteEngine on
RewriteRule ^/?profile/(\d+)/[^/]+/.+ /profile.php?do=view&profileid=$1&tab=2 [END]
RewriteRule ^/?profile/(\d+)/? /profile.php?do=view&profileid=$1 [END]

这篇关于htaccess 用第二个参数重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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