删除查询字符串并重定向(重写规则和重写cond) [英] Removing the the query string and redirecting (rewrite rule and rewrite cond)

查看:104
本文介绍了删除查询字符串并重定向(重写规则和重写cond)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试设置一个重写规则,以建立内部重定向,以实现对SEO更友好的url结构.

I am currently trying to set up a rewrite rule to set up an internal redirect for a more SEO-friendly url-structure.

我想展示:

/find-teacher/primary-school.php

代替悲伤的表情:

/find-teacher/subject-class-choose.php?school=primary-school

在阅读了有关重写规则和cond的一些帖子和教程之后,我想到了:

After I read some posts and tutorials about the rewrite rule and rewrite cond, I came up with this:

在我的choice-school.php中,我具有一个漂亮的URL链接:

In my choose-school.php I have as a link with the nice looking url:

<a href="https://www.example.com/find-teacher/primary-school.php">
Primary School</a>

在我的.htaccess中:

In my .htaccess:

RewriteCond %{REQUEST_URI} ^find-teacher/primary-school.php$
RewriteRule ^find-teacher/([a-z-]+)-([0-9]+).php$ /find-teacher/subject-class-choose.php?school=$1 [NC]

现在,如果我没做错,RewriteCond部分将检查是否请求了以下URL.如果是这种情况,那么([a-z-]+)-([0-9]+)的部分将被替换部分(/find-teacher/subject-class-choose.php?school=$1)的$1代替

Now if I got that right, the RewriteCond part checks if the following url is requested. If that is the case, then the part of ([a-z-]+)-([0-9]+) is replaced by the $1 of the substitution part (/find-teacher/subject-class-choose.php?school=$1)

问题是,如果我单击/find-teacher/primary-school.php

推荐答案

这里不需要RewriteCond和RewriteRule,因为它们(或应该)都在检查同一件事.

You don't need both a RewriteCond and a RewriteRule here, because they are (or should be) both checking the same thing.

但是,目前没有可以同时匹配这两种格式的URL.让我们来解决这个问题.

However, currently, there is no URL which will match both patterns. Let's break down the problem.

要专门检查URL /find-teacher/primary-school.php,您可以编写:

To check specifically for the URL /find-teacher/primary-school.php, you could just write:

RewriteRule ^find-teacher/primary-school.php$ /find-teacher/subject-class-choose.php?school=primary-school [NC]

下一步是使用()捕获"小学",并使用$1将其动态地放置到目标上:

The next step is to "capture" the "primary-school" with ( and ), and place it dynamically onto the target with $1:

RewriteRule ^find-teacher/(primary-school).php$ /find-teacher/subject-class-choose.php?school=$1 [NC]

最后,我们可以使用正则表达式[a-z-]+使模式匹配任何字母或连字符,而不是精确的字符串:

Finally, we can make the pattern match any letters or hyphens instead of an exact string, using the regex [a-z-]+:

RewriteRule ^find-teacher/([a-z-]+).php$ /find-teacher/subject-class-choose.php?school=$1 [NC]

在问题规则中,您已在正则表达式中添加了额外的部分:-([0-9]+).这匹配-,然后匹配数字.要捕获该数字,您可以将$2放在目标中的某个位置:

In the rule in your question, you've added an extra part to the regex: -([0-9]+). This matches - and then a number. To capture that number, you'd put $2 somewhere in your target:

RewriteRule ^find-teacher/([a-z-]+)-([0-9]+).php$ /find-teacher/subject-class-choose.php?school=$1&id=$2 [NC]

这将与/find-teacher/primary-school-123.php之类的东西匹配,然后将其转换为/find-teacher/subject-class-choose.php?school=primary-school&id=123.

This would match something like /find-teacher/primary-school-123.php, and turn it into /find-teacher/subject-class-choose.php?school=primary-school&id=123.

最后两个注意事项:

  • 取决于您的Apache配置设置,您是否可能需要在模式中包括前导/,即以^/开头
  • 我发现调试重写规则的一种好方法是通过将R=temp添加到标志(例如[NC,R=temp])来使它们重定向浏览器.这样,您不必依靠您的PHP代码即可告诉您所请求的URL
  • depending how your Apache configuration is set up, you may or may not need to include the leading / in your patterns, i.e. start them with ^/
  • I find a nice way to debug rewrite rules is to make them redirect your browser, by adding R=temp to the flags (e.g. [NC,R=temp]); that way, you don't have to rely on your PHP code to tell you what URL was requested

这篇关于删除查询字符串并重定向(重写规则和重写cond)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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