【问题标题】:.htaccess changes for redirecting requests to root of home page to a static html for wordpress.htaccess 更改将请求重定向到主页根目录到 wordpress 的静态 html
【发布时间】:2014-07-12 21:23:02
【问题描述】:

我有一个 Wordpress 设置,当用户输入站点的根目录到静态 HTML 文件 start.html 时需要重定向

http://www.myhomepage.com/

重定向到

http://www.myhomepage.com/start.html

Wordpress 为调用 index.php 添加 url 重写

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

我只想重定向

http://www.myhomepage.com/

而不是

http://www.myhomepage.com/buy/

这将不起作用,因为对 Wordpress 的所有请求都通过 index.php。

redirect /index.php /start.html

我想我需要重定向所有对 index.php 的纯请求,而不是那些带有查询字符串的请求。虽然我不知道如何重写它。

原因是我希望所有进入该站点的用户都能获得 wordpress 站点的静态 html。只有当用户开始浏览网站时,才应向 wordpress 提出请求。

编辑:我要求该规则仅适用于 GET 请求

【问题讨论】:

  • 为什么不设置DirectoryIndex
  • 这比下面的答案更简单。添加它,我也会接受它。谢谢!

标签: regex wordpress .htaccess mod-rewrite redirect


【解决方案1】:

它可以在没有 mod_rewrite 的情况下完成,使用 mod_dirDirectoryIndex 指令。

DirectoryIndex start.html

【讨论】:

  • 我最终需要一个比这更高级的解决方案。因为我只希望它用于 GET 请求而不是 POST。
【解决方案2】:

添加这一行:

RewriteRule ^/?$ /start.html [L]

就在这一行之后:

RewriteBase /

【讨论】:

  • 愿意解释一下吗?我想我需要阅读 RewriteRule。正则表达式 ^/?$ 匹配 mypage.com 但不匹配 mypage.com/buy
  • 您应该了解更多关于正则表达式 (regex) 的知识,它们非常方便。 ^/?$ 是正则表达式,^ 匹配开头,$ 匹配结尾,而 ?表示前面的字符是可选的,所以基本上该规则匹配任何只包含 / 或什么都不包含的 URL(域名不影响规则)
  • 我非常了解正则表达式,我阅读了 RewriteRule 以了解它是如何应用的,现在我理解了。我没有明白为什么 ^/$ 在使用带有斜杠的 mypage.com 时不起作用。
  • 因为某些版本的 apache(较新的)在重写规则中不包含斜杠(所以基本上 ^$ 应该使用或不使用斜杠,但我只是添加 /? 以确保它无处不在)
【解决方案3】:

除了@Oussama 解决方案之外,我还有一个在首页上发布的表单。发布到 start.html 将不起作用。我添加了一个条件,以便该规则仅适用于 GET 请求。这样表单帖子就会像往常一样被发送到 index.php。

RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?$ /start.html [L]

最终文件

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?$ /start.html [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

【讨论】:

    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多