htaccess 404 未发现问题?
如果找不到该页面,我想重定向到错误页面。所以我在 .htaccess 文件中添加了这段代码。
# '404 Not Found' error
ErrorDocument 404 /index.php?p=error&code=404
但是当我测试时它不会重定向到index.php 文件。
我的整个 .htaccess 文件代码在这里
#AuthName "Restricted Area"
#AuthType Basic
#AuthGroupFile /dev/null
#require valid-user
#<Files "/site/captcha/">
# Allow from all
#</Files>
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
## File paths are relative to the Document Root (/)
# '400 Bad Request' error
ErrorDocument 400 /index.php?p=error&code=400
# '404 Not Found' error
ErrorDocument 404 /index.php?p=error&code=404
# '403 Forbidden' error
ErrorDocument 403 /index.php?p=error&code=403
# '401 Unauthorized' error
ErrorDocument 401 /index.php?p=error&code=401
# '500 Internal Server Error'
ErrorDocument 500 /index.php?p=error&code=500
############################# Pages ##########################################################
RewriteRule ^home/$ index.php [S=1]
RewriteRule ^home$ index.php [S=1]
</IfModule>
i want to redirect to the error page if the page is not found. so i have add this code in .htaccess file.
# '404 Not Found' error
ErrorDocument 404 /index.php?p=error&code=404
but when i test it will not redirect to index.php file.
my whole .htaccess file code is here
#AuthName "Restricted Area"
#AuthType Basic
#AuthGroupFile /dev/null
#require valid-user
#<Files "/site/captcha/">
# Allow from all
#</Files>
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
## File paths are relative to the Document Root (/)
# '400 Bad Request' error
ErrorDocument 400 /index.php?p=error&code=400
# '404 Not Found' error
ErrorDocument 404 /index.php?p=error&code=404
# '403 Forbidden' error
ErrorDocument 403 /index.php?p=error&code=403
# '401 Unauthorized' error
ErrorDocument 401 /index.php?p=error&code=401
# '500 Internal Server Error'
ErrorDocument 500 /index.php?p=error&code=500
############################# Pages ##########################################################
RewriteRule ^home/$ index.php [S=1]
RewriteRule ^home$ index.php [S=1]
</IfModule>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先 - 将所有
ErrorDocument
指令移出
- 根本没有必要这样做。现在它告诉 Apache:“仅当 mod_rewrite 启用时才使用 ErrorDocument”。
如果它现在对您不起作用,那么很可能 mod_rewrite 实际上并未启用。但是,如果您将它们放在
块之外(就在它之前),那么它应该可以工作(只要 .htaccess 被 Apache 识别和处理):PS< /强>
我还修改了您的重写规则:将它们连接在一起形成单个规则。
To start with -- move all
ErrorDocument
directives out of<IfModule mod_rewrite.c>
-- there is no need at all for that.Right now it tells Apache: "use ErrorDocument ONLY IF mod_rewrite is enabled".
If it does not work for you right now, then it is extremely likely that mod_rewrite is not actually enabled. But if you place them outside
<IfModule mod_rewrite.c>
block (just before it), then it should work (as long as .htaccess is recognised and processed by Apache):P.S.
I have also modified your rewrite rules: jointed them together into single rule.