简体   繁体   English

在.htaccess中动态获取站点根主机名

[英]Get the site root host name dynamically in .htaccess

I am trying to make 404, 500 etc pages in ErrorDocument in php .htaccess file, it works fine if give 我试图在php .htaccess文件中的ErrorDocument中制作404,500个页面,如果给它,它工作正常

ErrorDocument 404 http://localhost/project/errordocs/404.html

but i do not want to hard code the url here, instead i want to get the root url site name dynamically so that i do not have change it again and again as the hostname changes. 但我不想在这里硬编码网址,而是我想动态获取根URL网站名称,以便我不会在主机名更改时一次又一次地更改它。

Basically i want to get this root url like: http://localhost/project can change to http://www.example1.com/project or http://www.example2.com/project etc. This url must come from projects root folder. 基本上我想得到这样的根URL: http://localhost/project可以更改为http://www.example1.com/projecthttp://www.example2.com/project等。这个url必须来自项目根文件夹。

So that will dynamically become: 这将动态变为:

ErrorDocument 404 http://localhost/project/errordocs/404.html
ErrorDocument 404 http://www.example1.com/project/errordocs/404.html
ErrorDocument 404 http://www.example2.com/project/errordocs/404.html

Any help please? 有什么帮助吗?

The asker asks incorrect. 提问者问不正确。 All what he writes 所有他写的东西

ErrorDocument 404 http://localhost/project/errordocs/404.html
ErrorDocument 404 http://www.example1.com/project/errordocs/404.html
ErrorDocument 404 http://www.example2.com/project/errordocs/404.html

may be done by 可以通过

ErrorDocument 404 /project/errordocs/404.html

But really he want: while moving site from project folder to project1, he should not change the rule 但他真的想要:在将网站从项目文件夹移动到project1时,他不应该改变规则

I think it may be done by htacces placed in /project with code 我认为可以通过放置在/ project中的htacces代码完成

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^  errordocs/404.html [L]

It will work if AllowOverride set to All (it is default). 如果AllowOverride设置为All(默认值),它将起作用。

The problem only that responce in this case will be 200 but not 404 只有在这种情况下响应的问题是200但不是404

Based on the conversation you had , you would like to not have to change the path to the error document when you move your project to another folder with a different name. 根据对话 ,您希望在将项目移动到具有不同名称的另一个文件夹时不必更改错误文档的路径。

Firstly, you cannot use variables when using ErrorDocument . 首先,使用ErrorDocument时不能使用变量。 The path that you provide must be static. 您提供的路径必须是静态的。 The path that you specify must be either to an external URL (in which case your browser will be redirected) or to a file relative to your document root (ie localhost ). 您指定的路径必须是外部URL(在这种情况下,您的浏览器将被重定向)或相对于文档根目录的文件(即localhost )。

Unfortunately, ErrorDocument won't be able to find the file relative to the current directory (ie project ). 不幸的是, ErrorDocument将无法找到相对于当前目录(即project )的文件。 The only logical way of doing this would be to remove the leading slash, but this would cause Apache to render that as a string in the browser. 执行此操作的唯一合理方法是删除前导斜杠,但这会导致Apache将其呈现为浏览器中的字符串。

This brings us to the only other possible solution: mod_rewrite . 这将我们带到唯一的其他可能的解决方案: mod_rewrite The only problem with using it, however, is that it is processed early on in the mapping pipeline which may allow other modules (such as mod_proxy) to affect the process . 然而,使用它的唯一问题是它在映射管道的早期处理,可能允许其他模块(例如mod_proxy)影响该过程

That said, you can try with the following: 也就是说,您可以尝试以下方法:

/project/.htaccess : /project/.htaccess

RewriteEngine on

# Determine if the request does not match an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# If so, send the request to the applicable file, relative to this directory
RewriteRule ^ errordocs/404.php [L]

# Per your comment and suggested edit, add the following.
# Note: This should not make any difference as mod_rewrite and PHP should
# already handle the error document.
ErrorDocument 404 /errordocs/404.php

/project/errordocs/404.php : /project/errordocs/404.php

This file will send the 404 header as .htaccess won't be able to do that. 此文件将发送404标头,因为.htaccess将无法执行此操作。

<?php header("HTTP/1.0 404 Not Found"); ?>

<h1>Sorry, we couldn't find that...</h1>
<p>The thing you've requested doesn't exist here. Perhaps it flew away?</p>

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

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