HOWTO重写引擎规则 [英] howto rewrite engine rule

查看:129
本文介绍了HOWTO重写引擎规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解一些规则或重写引擎,但我​​不明白怎么做了。

i'm trying to understand some rules or rewrite engine but i can't figure how to do it.

我有此链接:

w**.example.com/index.php?city=new+york

和我想改写这个新的:

w**.example.com/good-parties-in-new-york

城市价值可以换到其他任何城市。 但这里的问题不仅是我该怎么重写,如果所有的关键是:

the value of city can change to any other city. but the point here is I only what to rewrite if all the key is:

的index.php?城市=

因为

的index.php?区=

用于其他事情,等... 任何建议?谢谢。

is used for other things, etc... any suggestion? thanks.

推荐答案

我对究竟你想达到一个有点困惑。 URL重写通常做是为了网址看起来更好,而不是其他的方式。

I'm a little confused on what exactly you want to achieve. URL rewriting is normally done to make URLs look nicer, not the other way around.

您通常希望有一个漂亮的URL像这样(您想传达给你的用户):

You would typically want to have a nice URL like this (which you'd communicate to your users):

w**.example.com/good-parties-in-new-york

充当一个别名一个不那么好看的URL是这样的(=实际的页面被服务,不知情的用户):

act as an "alias" for a not-so-nice looking URL like this (= the actual page being served, unbeknownst to the users):

w**.example.com/index.php?city=new+york 


通过重写,你可以反向引用常规EX pressions。在这种情况下,你可以转换的好的网址为正则表达式的,你会再反向引用在后台工作,在页面的查询字符串变量的部分。


With Rewrite, you can backreference regular expressions. In this case, you could convert parts of the "nice" URL into RegEx's which you would then backreference with variables in the query string of the page working in the background.

例如:

RewriteEngine On

RewriteBase /
RewriteRule ^good-parties-in-([a-z]+)-([a-z]+)$ index.php?city=$1+$2 [NC,L]

第一个正则表达式(([AZ] +))被引用为 $ 1 ,第二个为 $ 2 (依此类推)。

The first RegEx (([a-z]+)) is referenced as $1, the second as $2 (and so on).

请注意,这个例子将只对城市名称组成的两个词,如纽约,旧金山等,你必须弄清楚有多少的话城市名称可以包含并相应地重写你的code 。 (您可能还需要设置不同的标志。)

Note that this example will only work for city names consisting of two words, like New York, San Francisco etc. You'll have to figure out of how many words city names can consist of and rewrite your code accordingly. (You might also have to set different flags.)

另外,你应该确保对现有城市的名字你的PHP脚本检查,并引发相应的错误/给出了一个警告,如果用户输入想好缔约国,在神奇的彩虹,城市或类似的幻想的名字。

Plus, you should make sure that your php script checks against existing city names and throws an appropriate error/gives out a warning if users enter fantasy names like good-parties-in-magical-rainbow-city or similar.

如果这不是你要找的是什么,也许你可以澄清你的问题?

If this isn't what you're looking for, maybe you could clarify your question?

这篇关于HOWTO重写引擎规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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