【问题标题】:htaccess consolidate root to url and hide index.phphtaccess 将 root 合并到 url 并隐藏 index.php
【发布时间】:2014-11-06 21:22:01
【问题描述】:

我有这两个 htaccess 重写(我从其他地方挑选出来的),它们独立地为我的站点执行 X 或 Y。我想不通的是如何编写它以使两个功能都能正常工作。

重定向所有缺少“www”的请求:

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

重定向所有包含“index.php”的请求:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

此外,前一个函数用于将 example.com 重写为 www.example.com。但是,我在这个根目录的子目录中有另一个站点,它有自己的“独立”url。但该函数还会将该 URL 从 www.anotherexample.com 重写为 www.example.com/anotherexample。

如何在不重写我的子目录站点 url 的情况下将这些合并为一个可行的函数?提前致谢。

【问题讨论】:

  • 您的 htaccess 文件中还有其他规则吗?

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

您可以将这些组合规则保存在根 .htaccess 中:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

【讨论】:

  • 做到了,阿努巴瓦。 :) 非常感谢!
猜你喜欢
  • 2015-07-21
  • 2016-12-26
  • 2020-04-03
  • 1970-01-01
  • 2014-03-13
  • 2015-07-05
  • 2015-12-19
  • 1970-01-01
  • 2013-10-08
相关资源
最近更新 更多