[英]Removing the the query string and redirecting (rewrite rule and rewrite cond)
I am currently trying to set up a rewrite rule to set up an internal redirect for a more SEO-friendly url-structure. 我目前正在尝试设置一个重写规则,以设置内部重定向以实现对SEO更友好的url结构。
I want to show: 我想展示:
/find-teacher/primary-school.php
instead of the sad looking: 而不是悲伤的样子:
/find-teacher/subject-class-choose.php?school=primary-school
After I read some posts and tutorials about the rewrite rule and rewrite cond, I came up with this: 在阅读了有关重写规则和cond的一些帖子和教程之后,我想到了:
In my choose-school.php I have as a link with the nice looking url: 在我的choice-school.php中,我具有一个漂亮的URL链接:
<a href="https://www.example.com/find-teacher/primary-school.php">
Primary School</a>
In my .htaccess: 在我的.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]
Now if I got that right, the RewriteCond
part checks if the following url is requested. 现在,如果我理解正确,则
RewriteCond
部分将检查是否请求了以下URL。 If that is the case, then the part of ([az-]+)-([0-9]+)
is replaced by the $1
of the substitution part ( /find-teacher/subject-class-choose.php?school=$1
) 如果是这种情况,则
([az-]+)-([0-9]+)
的部分将替换为替换部分的$1
( /find-teacher/subject-class-choose.php?school=$1
)
Problem is that I am redirected to my error page if I click on /find-teacher/primary-school.php
问题是,如果我单击
/find-teacher/primary-school.php
重定向到错误页面。
You don't need both a RewriteCond and a RewriteRule here, because they are (or should be) both checking the same thing. 您在这里不需要RewriteCond和RewriteRule,因为它们(或者应该)都在检查同一件事。
However, currently, there is no URL which will match both patterns. 但是,当前没有可以同时匹配两种模式的URL。 Let's break down the problem.
让我们来解决这个问题。
To check specifically for the URL /find-teacher/primary-school.php
, you could just write: 要专门检查URL
/find-teacher/primary-school.php
,您可以编写:
RewriteRule ^find-teacher/primary-school.php$ /find-teacher/subject-class-choose.php?school=primary-school [NC]
The next step is to "capture" the "primary-school" with (
and )
, and place it dynamically onto the target with $1
: 下一步是使用
(
和)
“捕获”“小学”,并使用$1
将其动态地放置到目标上:
RewriteRule ^find-teacher/(primary-school).php$ /find-teacher/subject-class-choose.php?school=$1 [NC]
Finally, we can make the pattern match any letters or hyphens instead of an exact string, using the regex [az-]+
: 最后,我们可以使用regex
[az-]+
使模式匹配任何字母或连字符而不是确切的字符串:
RewriteRule ^find-teacher/([a-z-]+).php$ /find-teacher/subject-class-choose.php?school=$1 [NC]
In the rule in your question, you've added an extra part to the regex: -([0-9]+)
. 在问题的规则中,您已在正则表达式中添加了额外的部分:
-([0-9]+)
。 This matches -
and then a number. 该匹配
-
然后是一个数字。 To capture that number, you'd put $2
somewhere in your target: 要获取该数字,您可以将
$2
放置在目标中的某个位置:
RewriteRule ^find-teacher/([a-z-]+)-([0-9]+).php$ /find-teacher/subject-class-choose.php?school=$1&id=$2 [NC]
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
. 这将匹配
/find-teacher/primary-school-123.php
类的/find-teacher/primary-school-123.php
,并将其转换为/find-teacher/subject-class-choose.php?school=primary-school&id=123
。
Two final notes: 最后两个注意事项:
/
in your patterns, ie start them with ^/
/
,即以^/
开头 R=temp
to the flags (eg [NC,R=temp]
); R=temp
添加到标志(例如[NC,R=temp]
)来使它们重定向浏览器。 that way, you don't have to rely on your PHP code to tell you what URL was requested
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.