简体   繁体   English

Ubuntu 13.10-Apache2-自定义404-不带有.htaccess

[英]Ubuntu 13.10 - Apache2 - Custom 404 - not with .htaccess

So I've spent a fair bit of trying and reading including the http://httpd.apache.org/docs/2.2/custom-error.html 因此,我花了很多时间进行尝试和阅读,其中包括http://httpd.apache.org/docs/2.2/custom-error.html

and I sadly can't get it all to align. 而我可悲的是,一切都无法统一。

What I'm trying to do (with Ubuntu 13.10 & Apache2) is the following: Use virtualhosts. 我想做的事情(使用Ubuntu 13.10和Apache2)如下:使用virtualhosts。 Have the default site and all virtual hosts return a custom 404 page. 使用默认站点,所有虚拟主机均返回自定义404页面。

This 404 / html page has an image it loads as the background, so in the Apache root directory (/var/www) I have: 404.html [images]\\404-background.jpg 此404 / html页面具有作为背景加载的图像,因此在Apache根目录(/ var / www)中,我具有:404.html [images] \\ 404-background.jpg

I've tried so many iterations (and continue to while hoping for help) that I'm not going to list them all here. 我已经尝试了很多次迭代(并在希望获得帮助的同时继续进行),以至于不在这里列出所有迭代。 I'd like to do this within the config files and not .htaccess files. 我想在配置文件而不是.htaccess文件中执行此操作。

Can someone point me in the right direction please? 有人可以指出我正确的方向吗? I apologize for the similar questions but they seem to refer to previous versions of Ubuntu / Apache (for example the new default .conf extension) 我为类似的问题表示歉意,但它们似乎是指Ubuntu / Apache的早期版本(例如新的默认.conf扩展名)

I have edited /etc/apache2/conf-enabled/localized-error-pages.conf with 我用/编辑了/etc/apache2/conf-enabled/localized-error-pages.conf

ErrorDocument 404 /404.html

The 404 works on the default but not in the virtualhost. 404适用于默认主机,但不适用于虚拟主机。 I'm likely missing an directive in the VH config file. 我可能在VH配置文件中缺少指令。

The issue stems from the VH referencing its own DocumentRoot. 该问题源于VH引用其自己的DocumentRoot。 In the HTML of the 404.html page I have: 在404.html页面的HTML中,我有:

<div id="background">
    <img src="images/404-background.jpg" class="stretch" alt="404 - Page not found." >
</div>

If I try an Alias approach I can get the 404.html page to load but the image reference doesn't work (because it is looking at the VH as the base) 如果我尝试使用Alias方法,则可以加载404.html页面,但是图像引用不起作用(因为它将VH作为基础)

Example VH.conf that won't reference the image: 不引用图像的示例VH.conf:

Alias "/error" "/var/www/"
ErrorDocument 404 /error/404.html

I've tried approaching this from 我尝试过从

/etc/apache2/conf-available/localized-error-pages.conf 

which is probably what it will take but haven't yet sorted it. 这可能是需要采取的措施,但尚未进行排序。

Many thanks. 非常感谢。

In your VirtualHost directives for the site, you could add the ErrorDocument 404 /404.html in each of them. 在该站点的VirtualHost伪指令中,可以在每个伪伪指令中添加ErrorDocument 404 /404.html But you would need the 404.html document to be put in each of the VirtualHosts document root. 但是您需要将404.html文档放入每个VirtualHosts文档根目录中。 ex If I have a VirtualHost for the filepath /var/www/test then you would put the 404 HTML document in the root of that directory. 例如,如果我为文件路径/var/www/test提供了VirtualHost,则可以将404 HTML文档放在该目录的根目录中。

OK - the following worked (although I wish it took a little less effort): 好的-以下工作有效(尽管我希望此工作能省些):

Copy the 404.html file to 将404.html文件复制到

/usr/share/apache2/error

If it is a simple page, you nearly done. 如果页面很简单,那么您几乎完成了。 Because I used CSS and Images it required a bit more work. 因为我使用CSS和Images,所以需要做更多的工作。

In your /etc/apache2/conf-available/localized-error-pages.conf add your new file for example: 在/etc/apache2/conf-available/localized-error-pages.conf中,添加新文件,例如:

Alias /error/ "/usr/share/apache2/error/"
ErrorDocument 404 /error/404.html

Now if you are using CSS and Images you have to create Aliases in each virtual host. 现在,如果您正在使用CSS和图像,则必须在每个虚拟主机中创建别名。

For example in SomeVirtualHost.conf 例如在SomeVirtualHost.conf中

Alias /images/ "/var/www/images/"
Alias /css/ "/var/www/css/"

Beware of any conflicts that may arise from duplicate folder naming. 注意重复的文件夹命名可能引起的任何冲突。 After considering that I added a folder /404resources/ and moved the 2 dependent folders. 考虑完之后,我添加了一个文件夹/ 404resources /并移动了2个从属文件夹。 This required an update to the 404.html page as well 这也需要更新404.html页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM