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 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥15 MATLAB数据处理插值
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的
  • ¥15 硬件IIC从模式的数据发送,中断数据的接收,不能用HAL库(按照时序图)