【问题标题】:htaccess using RewriteRule and RewriteCond to redirect specific urlhtaccess 使用 RewriteRule 和 RewriteCond 重定向特定的 url
【发布时间】:2017-08-13 03:08:55
【问题描述】:

我必须从

重定向 (301)

http://domain/index.phphttp://domain/

我已经按照以下规则完成了:

RewriteCond %{THE_REQUEST} ^.*/index.php

重写规则 ^(.*)index.php$ /$1 [R=301,L]

好的。它有效,但我不想允许从以下网址重定向:

http://domain/asd/index.php

我试图改变条件:

RewriteCond %{THE_REQUEST} ^/index.php

RewriteCond %{THE_REQUEST} ^/index.php

但没有成功。

那么,如何仅从http://domain/index.php 重定向到http://domain/

ps:我不想使用 REDIRECT 命令

【问题讨论】:

  • RewriteCond %{THE_REQUEST} ^/index.php
  • 你为什么不简单地使用 apache 的DirectoryIndex 指令呢? httpd.apache.org/docs/2.4/mod/mod_dir.html
  • Unamata Sanatarai - 就像我写的那样,它不起作用
  • arkascha - 我不知道,谢谢!

标签: .htaccess mod-rewrite


【解决方案1】:

试试这个:

RewriteRule ^index.php$ / [R=301,L]

【讨论】:

  • 这项工作完全符合我的预期!非常感谢。
  • 当然我必须把我的 RewriteCond 放在它前面。
  • 你不需要它。
  • 我刚刚又检查了一遍。如果我在重定向所有传入请求之前没有将 RewriteCond %{THE_REQUEST} ^.*/index\.php 放入行中。所以我需要添加它。
【解决方案2】:

您可以使用以下规则:

RewriteEngine on

RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ / [L,R]

【讨论】:

  • @Kriss 我发布的解决方案对我来说效果很好。它与接受的答案相同。不知道你为什么说“它不工作”。
  • 我将它复制并粘贴到我的 .htaccess 中,但它没有达到我的预期,所以正如我所写的,“不适合我”。我相信它可以在其他.htaccess 中工作。我的 .htaccess 是来自 PrestaShop 的文件。也许这就是原因。
猜你喜欢
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多