【问题标题】:.htaccess that redirects to index.html, or to index.php if there are parameters.htaccess 重定向到 index.html,如果有参数,则重定向到 index.php
【发布时间】:2013-11-06 16:47:56
【问题描述】:

我正在寻找执行以下操作的.htaccess

  1. 对于访问mydomain.com的用户,服务器应该从根目录返回index.html文件。

  2. 对于访问mydomain.com/something/mydomain.com/anything/123/mydomain.com/some%20encoded%20text等的用户,服务器应从根目录返回index.php文件,并将www.mydomain.com/后面的文本作为PHP@987654329传递@变量。

我试过 WordPress .htaccess:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

但这会将所有内容重定向到 index.php 文件。

【问题讨论】:

  • 为什么不直接将所有流量导向 index.php,如果没有额外的 URI 传递,只提供 index.html 内容?网站中除一个之外的所有页面都有一个前端控制器似乎很奇怪。
  • @MikeBrant 我希望大多数流量直接来到 mydomain.com,所以我认为提供静态 html 文件比提供 php 更好。
  • 我添加了最初尝试的解决方案。

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

启用mod_rewrite.htaccesshttpd.conf(如果尚未启用),然后将此代码放入您的DOCUMENT_ROOT/.htaccess 文件中:

RewriteEngine On

RewriteRule ^$ /index.html [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?f=$1 [L,QSA]

index.php 中,您可以访问$_GET['f'] 变量,该变量将为您提供请求的URI。

【讨论】:

  • 谢谢,效果很好。在这种情况下,您会为 $_GET['f'] 推荐哪种 PHP 清理方法,以防止恶意代码? (我将使用该变量作为纯字符串来计算它的哈希值)
  • 不客气。请参阅此问题进行消毒:stackoverflow.com/questions/205923/…
  • 如果编码后的url包含换行符,例如mydomain.com/one%0Atwo似乎不起作用我得到ERROR 404 - PAGE NOT FOUND我>
  • 使用编码字符:RewriteRule ^(.+)$ /index.php?f=$1 [L,QSA,B,NE]
  • 不幸的是,它不适用于换行符 %0A,我仍然收到 404 错误。
猜你喜欢
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
  • 2013-01-21
  • 2014-08-31
  • 2010-10-27
  • 1970-01-01
相关资源
最近更新 更多