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 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法