【问题标题】:htaccess redirect to static html hosted within Wordpress roothtaccess 重定向到托管在 Wordpress 根目录中的静态 html
【发布时间】:2018-02-09 11:27:53
【问题描述】:

我目前在我的 Wordpress 安装的根文件夹中托管了一个静态 HTML 文件。我可以访问 URL http://example.com/er_trends_download,但在 URL 添加尾部斜杠时出现内部服务器错误:http://example.com/er_trends_download/

我在.htaccess 文件中添加了几行以从我的静态文件中删除.html,并且我在WordPress 中的永久链接设置也设置为删除尾部斜杠。我对是否保留斜杠没有偏好,我只是不想得到内部服务器错误,无论它是否是 URL 的一部分。我在下面包含了我的完整 .htaccess 文件:

# Use PHP5.6 as default
AddHandler application/x-httpd-php56 .php

php_value memory_limit 512M

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
</IfModule>


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

# END WordPress

【问题讨论】:

    标签: wordpress .htaccess redirect mod-rewrite url-rewriting


    【解决方案1】:
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^ %{REQUEST_URI}.html [NC,L]
    

    此代码的问题在于,当请求以斜杠结束时(例如http://example.com/er_trends_download/),REQUEST_FILENAME 服务器变量不包括尾部斜杠,但 REQUEST_URI 变量包含。因此,您最终会重写为/er_trends_download/.html(这显然是错误的),但这会导致重写循环....../er_trends_download/.html.html/er_trends_download/.html.html.html 等等,服务器最终会因 500 错误而中断。

    请尝试以下方法:

    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+?)/?$ /$1.html [L]
    

    这使得尾部斜杠是可选的,非贪婪模式.+? 确保我们抓取所有内容,但不包括尾部斜杠(如果有的话)。此处不需要NC 标志。

    因此,这允许 URL 带有或不带有斜杠(这可能是可取的,也可能不是可取的)。

    【讨论】:

    • 谢谢!这成功了,我只需将您的代码建议中的 .php 更改为 .html 如果您可以在您的回复中进行此编辑,我会将其标记为已接受。
    • 不客气。抱歉,是的,.php 是习惯的力量(现已更新)!
    猜你喜欢
    • 2014-07-12
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多