我的重写规则不起作用


My rewrite rule is not working

我需要为页面制定重写规则,但它不起作用。我确实启用了 apache mod_rewrite

这是我的.htacces文件:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^gameofthrones/(full|house|characters)'.(all|Stark|Lannister)'.(html|xml|json)$ index.php?output=$3&house=$2&info=$1
</IfModule>

但是当我输入这个网址时:localhost/school/str-webservices/eindopdracht/index.php?output=html&house=all&info=full

它保持这种状态,但它应该是这样的:localhost/school/str-webservices/eindopdracht/gameofthrones/full/all/html

我做错了什么?

提前感谢,

马克

你误解了URL重写的概念。

假设我重写/hello index.php?get=hello.这意味着index.php?get=hello仍然可以工作,但是如果我写/hello,它将在引擎盖下将其映射到index.php?get=hello

因此,当用户的地址栏显示/hello时,他实际上正在查看index.php?get=hello

这不会取消index.php?get=hello,这并不意味着如果有人通过该URL访问该页面,它将用户重定向到/hello

添加另一个规则,该规则实际上将未重写的URL重定向到重写的表单:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /school/str-webservices/eindopdracht/
    RewriteCond %{REQUEST_URI} ^school/str-webservices/eindopdracht/index'.php
    RewriteCond %{QUERY_STRING} ^output=([^&]*)&house=([^&]*)&info=([^&]*)$
    RewriteRule ^(.*)$ gameofthrones/%3.%2.%1? [R=301,L]
    RewriteRule gameofthrones/(full|house|characters)'.(all|Stark|Lannister)'.(html|xml|json)$ index.php?output=$3&house=$2&info=$1 [PT,NC,L]
</IfModule>

然后调用:localhost/school/str-webservices/eindopdracht/index.php?output=html&house=all&info=full应该将您重定向到:localhost/school/str-webservices/eindopdracht/gameofthrones/full.all.html

我认为这可能是目录问题:

  • .htaccess 文件位于目录树中的什么位置?
  • 索引.php 所在的文件在哪里?

如果 index.php 文件位于同一个目录中(我假设相同的 ./school/str-webservices/eindopdracht/),您只需添加:

RewriteBase /school/str-webservices/eindopdracht/
after RewriteEngine On

如果我不明白这个问题,我很抱歉。

再见。