Php Htaccess-Add.html(需要时)-Wordpress

Php Htaccess-Add.html(需要时)-Wordpress,php,html,wordpress,.htaccess,Php,Html,Wordpress,.htaccess,我想将.html扩展添加到所有未使用此扩展完成的页面。我想在带有.html的页面存在时执行此操作。 我已经尝试了很多我见过的代码,但在这个意义上没有任何效果。也许有一些不同于htaccess的东西可以做到这一点。 我得到的唯一一件事就是在页面以/结束时添加.html扩展名。 这个网站是用wordpress制作的,所以如果你知道任何插件或者其他可以做到这一点的东西,请告诉我:) 我在.htaccess中的初始代码是: # Do not do anything for already existin

我想将.html扩展添加到所有未使用此扩展完成的页面。我想在带有.html的页面存在时执行此操作。 我已经尝试了很多我见过的代码,但在这个意义上没有任何效果。也许有一些不同于htaccess的东西可以做到这一点。 我得到的唯一一件事就是在页面以/结束时添加.html扩展名。 这个网站是用wordpress制作的,所以如果你知道任何插件或者其他可以做到这一点的东西,请告诉我:)

我在.htaccess中的初始代码是:

# Do not do anything for already existing files and folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

# add .html file extension (if such file does exist)
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+[^/])/?$ $1.html [L,QSA]
非常感谢你的回答

非常简单:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# If /foo/bar does not exist as a file (and)
RewriteCond %{REQUEST_FILENAME} !-f
# If /foo/bar does not exist as a directory (and)
RewriteCond %{REQUEST_FILENAME} !-d
# If /foo/bar.html exists as a file
RewriteCond %{REQUEST_FILENAME}.html -f
# If above three conditions match, append .html
# stopping any other rewrite rules below this (L=last)
# and adding query string (QSA=Query String Append) such as ?a=b&c=d
RewriteRule ^(.+)$ /$1.html [L,QSA]

问题是有好几次我不需要扩展名,所以我找到了一个wordpress插件可以做到这一点,我还在php中使用了一个小函数,检查url中的单词是否添加.html扩展名

我使用的插件的名称是:

通过在页面上添加扩展名

该功能与此类似:

function my_page_template_redirect()
{
    global $wp;
    $current_url = home_url(add_query_arg(array(),$wp->request));   
    if ( !strpos($current_url, '.html') && !strpos($current_url, 'wp-admin') && !strpos($current_url, 'wp-login') ) {
        if(!is_front_page()){
            wp_safe_redirect( $current_url.'.html' );   
        }       
    }   
}
add_action( 'template_redirect', 'my_page_template_redirect' );

谢谢你的帮助

您需要确保
RewriteCond
测试
-f
“是一个文件”。这就是你的暗示;)下次当你询问时,请添加你迄今为止尝试过的代码。这不起作用!感谢您的回答,但是当我编写不带.html扩展名的URL时,它会将我返回到404。URL是否与注释中所写的所有条件匹配?问题是具有html扩展名的文件不存在。我与wordpress合作,我认为我必须对每个案例使用一个正则表达式。你知道如何在.httaccess中使用正则表达式吗?你不是这样说的,“我想在有.html的页面存在时使用它”。如果您使用的是wordpress,则只需更改永久链接结构(设置>永久链接)。Wordpress路由服务器上物理上不存在的任何请求,以便为正确的页面提供服务器。如果您更改永久链接,wordpress将自动更新菜单中的超链接。我知道,我已将扩展名.html添加到所有页面,但我想要的是,当URL没有.html扩展名且与.html一起存在时,我希望将其重定向到带有.html的URL。该解决方案适用于页面,但是,预览帖子时可能会出现问题。如何克服这一点?抱歉@sumithauhan,但我没有在那个项目上使用预览,所以对我来说工作很有魅力。