问题描述
目前每个无效页面都是 500(内部服务器错误),因为我可能搞砸了我的服务器块配置.
Currently every invalid page is 500 (Internal Server Error) because I probably messed up with my server block configuration.
不久前我决定关闭我的网站并创建了一个简单的单页感谢主页.然而,旧链接和外部站点仍在尝试访问该站点的其他部分,这些部分不再存在.
I decided to shut down my website a while ago and created a simple one-page, thank-you homepage. However old links and external sites are still trying to access other parts of the site, which no longer exists.
如何强制将所有非主页(任何无效 URL)重定向到主页?
How do I force redirect all non-homepage (any invalid URL) to the homepage?
我尝试了以下块,但没有奏效:
I tried with the following block, but it didn't work:
location / {
try_files $uri $uri/ $document_uri/index.html;
}
我当前的配置是(我现在甚至不提供 PHP 文件,即主页是简单的 html):
My current configuration is (I don't even serve PHP files right now, ie homepage is simple html):
server {
server_name www.example.com example.com;
access_log /srv/www/example.com/logs/access.log;
error_log /srv/www/example.com/logs/error.log;
root /srv/www/example.com/public_html;
index index.php index.html;
location / {
try_files $uri $uri/ $document_uri/index.html;
}
# Disable favicon.ico logging
location = /favicon.ico {
log_not_found off;
access_log off;
}
# Allow robots and disable logging
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Enable permalink structures
if (!-e $request_filename) {
rewrite . /index.php last;
}
# Handle php requests
location ~ .php$ {
try_files $uri = 404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_send_timeout 900;
fastcgi_read_timeout 900;
fastcgi_connect_timeout 900;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# Disable static content logging and set cache time to max
location ~* .(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires max;
}
# Deny access to htaccess and htpasswd files
location ~ /.ht {
deny all;
}
# Deny access to hidden files (beginning with a period)
location ~ /. {
access_log off; log_not_found off; deny all;
}
}
推荐答案
像这样设置错误页面到首页
Setting the error page to the home page like this
error_page 404 /index.html;
有个小问题,首页的状态码会是404 not found",如果你想加载带有200 ok"状态码的首页,你应该这样做
has a small problem, the status code of the home page will be "404 not found", if you want to load the home page with a "200 ok" status code you should do it like this
error_page 404 =200 /index.html;
这会将404 not found"错误代码转换为200 ok"代码,并加载主页
This will convert the "404 not found" error code to a "200 ok" code, and load the home page
@jvperrin 提到的第二种方法也不错,
The second method which @jvperrin mentioned is good too,
try_files $uri $uri/ /index.html;
但您需要记住一件事,因为它是 location/
任何与另一个位置不匹配且未找到的资产也将加载 index.html
,例如缺少图像、css、js 文件,但在您的情况下,我可以看到您已经有另一个与资产扩展名匹配的位置,因此您不应该遇到这个问题.
but you need to keep 1 thing in mind, since it's the location /
any asset that doesn't match another location and is not found will also load the index.html
, for example missing images, css, js files, but in your case I can see you already have another location that's matching the assets' extensions, so you shouldn't face this problem.
这篇关于如何强制将所有 404(或每个页面,无论是否无效)重定向到主页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!