dra8603 2017-04-05 16:11
浏览 29
已采纳

删除查询字符串并重定向(重写规则和重写)

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

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:

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>

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]

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)

Problem is that I am redirected to my error page if I click on /find-teacher/primary-school.php

  • 写回答

1条回答 默认 最新

  • duanhao4156 2017-04-05 17:26
    关注

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

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

    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]
    

    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]
    

    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]
    

    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]
    

    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.

    Two final notes:

    • 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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog