php 404 未找到页面不适用于我的 .htaccess

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17639561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:12:41  来源:igfitidea点击:

404 not found page not working with my .htaccess

phphttp-status-code-404

提问by Eric T

I have below .htaccess on the server with the not found page rules but it doesn't seems to work. Any idea?

我在服务器上的 .htaccess 下有未找到的页面规则,但它似乎不起作用。任何的想法?

RewriteEngine on
RewriteRule ^article_detail/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ article_detail.php?article_det=
Options +FollowSymLinks
ErrorDocument 404 http://www.example.com/main/404_not_found.html


# compress text, HTML, JavaScript, CSS, and XML

<IfModule mod_ext_filter.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
# remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

回答by

I'm assuming you're using Apache rather than IIS;

我假设您使用的是 Apache 而不是 IIS;

  1. Ensure you have mod rewrite enabled(You can do this by locating the line below in the httpd.conffile (usually within the /conf/directory This maybe different if you're using shared hostingand removing the semi colon from the start of the line)

    ;LoadModule rewrite_module modules/mod_rewrite.so

  2. Find the relevant directory tag for your www root and change AllowOverride Noneto AllowOverride All

  3. Create an .htaccess file within the root directory (If one doesn't exist)

  4. You'd need to insert something that looks like this;

    ErrorDocument 500 /errordocs/500.html

    ErrorDocument 404 /errordocs/404.html

    ErrorDocument 401 /errordocs/401.html

    ErrorDocument 403 /errordocs/403.html

    • Instead of ErrorDocument 404 /errordocs/404.htmlyou could also do ErrorDocument 404 http://www.yourdomain.com/errordocs/404.htmlor even just display a simple message ErrorDocument 404 "Sorry can't allow you access today"

    • If you have any trouble the Apache Manual (ErrorDocument Directive)should help you out further.

  1. 确保您启用了 mod 重写(您可以通过在httpd.conf文件中找到下面的行(通常在/conf/目录中)来做到这一点,如果您使用共享主机并从行的开头删除分号,这可能会有所不同

    ;LoadModule rewrite_module modules/mod_rewrite.so

  2. 找到与您的 www 根目录相关的目录标签并更改AllowOverride NoneAllowOverride All

  3. 在根目录中创建一个 .htaccess 文件(如果不存在)

  4. 你需要插入看起来像这样的东西

    ErrorDocument 500 /errordocs/500.html

    ErrorDocument 404 /errordocs/404.html

    ErrorDocument 401 /errordocs/401.html

    ErrorDocument 403 /errordocs/403.html

    • 而不是ErrorDocument 404 /errordocs/404.html你也可以做ErrorDocument 404 http://www.yourdomain.com/errordocs/404.html甚至只是显示一个简单的消息ErrorDocument 404 "Sorry can't allow you access today"

    • 如果您遇到任何问题,Apache 手册(错误文档指令)应该会进一步帮助您。

In your case make sure AllowOverride Allis on, your editing the .htaccessfile in the root and make sure you can hit the URL for the error page, if there's nothing at the URL that you specify Apache will return the default 404.

在您的情况下AllowOverride All,请确保已打开,您.htaccess在根目录中编辑文件并确保您可以点击错误页面的 URL,如果您指定的 URL 中没有任何内容,Apache 将返回默认的 404。

回答by Ga?tan

In your apache configuration, in the Directory section of your website, be sure that you are allowed to override the FileInfo directive.

在您的 apache 配置中,在您网站的目录部分,确保您可以覆盖 FileInfo 指令。

Somthing like

有点像

AllowOverride FileInfo

允许覆盖文件信息

http://httpd.apache.org/docs/current/mod/core.html#errordocument

http://httpd.apache.org/docs/current/mod/core.html#errordocument

回答by Jasmin Mistry

Try to uncomment line from httpd.conf in apache configuration.

尝试从 apache 配置中的 httpd.conf 中取消注释行。

#LoadModule rewrite_module modules/mod_rewrite.so

is uncommented:

未注释:

LoadModule rewrite_module modules/mod_rewrite.so

Also restart apache

也重启apache

回答by Ross Logan

Have you tried ErrorDocument 404 /main/404_not_found.html?

你试过ErrorDocument 404 /main/404_not_found.html吗?

Just incase you have something interfering with your URL (not sure if that is your complete htaccess)? I usually don't put the whole URL in although technically you can.

以防万一您的网址受到干扰(不确定这是否是您的完整 htaccess)?我通常不会把整个 URL 放在里面,虽然技术上你可以。

Also can you access the 404 page directly?

也可以直接访问404页面吗?

回答by David Level

Try to change the line by ErrorDocument 404 http://www.google.com

尝试通过 ErrorDocument 404 http://www.google.com更改该行

If that works, that mean that the path you are using was misconfigure. I don't know your directory structure so I can give you the good code to write.

如果可行,则意味着您使用的路径配置错误。我不知道你的目录结构,所以我可以给你写好的代码。

I don't think it's an .htaccess error because you'll get a 500 Internal error if it is.

我不认为这是 .htaccess 错误,因为如果是,您将收到 500 Internal 错误。

If this tricks does not work, that means something redirecting inside your apache configuration.

如果此技巧不起作用,则意味着在您的 apache 配置中进行了重定向。