.html导致404错误

时间:2014-02-03 16:02:38

标签: php html nginx joomla2.5

我在ubuntu服务器上使用php / nginx / mysql运行一个joomla 2.5站点(相当新的nginx)

我的问题是,当用户点击网址时,我需要它来忽略.html文件扩展名。

例如,如果您点击mysite.com/page,则会将页面呈现为正常。

如果你点击mysite.com/page.html那么它会抛出404错误。这是因为我的网站上实际上没有“page.html”页面。它是K2的别名。是的,我不能放入.html,但它不是我向网站添加内容,而是客户端。我最近移动了服务器,在它工作正常之前,现在它没有这样我知道我在配置中遗漏了一些东西。

我知道我可以通过try files让nginx与我想要的相反。不知道如何让它反过来。

这是我的nginx配置:

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to index.html
    try_files $uri $uri/ /index.php;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
}

现在我知道我是否有一个page.html页面,我想隐藏.html我可以添加$uri.html

我想知道的是如何让nginx试用page.html如果它找不到page

3 个答案:

答案 0 :(得分:1)

原来是Joomla Config变量。在全局配置中的SEO设置下设置'apply suffix'为yes。我觉得很蠢。感谢所有那些试图提供帮助的人,一如既往地非常感谢。

答案 1 :(得分:0)

这是我设法遇到的潜在解决方案:

location / {
    # If /{foo} is not an existing file or directory, rewrite to /{foo}.html
    if (!-e $request_filename) {
        rewrite ^(.+)$ /$1.html break;
    }
    # Redirect all {foo}.html URLS to /{foo}
    rewrite ^/(.*)\.html$ /$1 redirect;
}

如果您能正确撰写.htaccess重写行,请尝试使用htaccess-to-nginx converter

如评论所述,这将使用当前URL检查目录,文件等的存在。如果不存在,它将尝试请求附加.html的网址。

此外,如果网址中已存在.html,则会将其重定向到不包含.html的网址

答案 2 :(得分:0)

快速解决方法:

rewrite ^(. *)\.html$ $1 break;

应该是堆栈中的第一次重写,甚至在try_files之前。

相关问题