![](/img/trans.png)
[英]Difference between: $_SERVER['SCRIPT_NAME'] and $_SERVER['PHP_SELF']
[英]SCRIPT_NAME and PHP_SELF with mod_rewrite in .conf
將此mod_rewrite從.htaccess
移至apache2.conf
, $_SERVER['SCRIPT_NAME']
和$_SERVER['PHP_SELF']
返回不同的值。
# point all requests to index.php
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L,NS]
網址:
http://domain.com/path/to/dir/
如果重寫是在.htaccess
則$_SERVER['PHP_SELF']
返回/index.php
如果重寫在apache2.conf
則$_SERVER['PHP_SELF']
返回/path/to/dir/
$_SERVER['PHP_SELF']
不起作用時,如何獲取文檔根目錄的相對路徑?
要獲取文檔根目錄的相對路徑,可以使用$_SERVER['DOCUMENT_ROOT']
例如用法:
define('APP_BASE', $_SERVER['DOCUMENT_ROOT']);
或者你也可以像這樣使用realpath(dirname(__FILE__))
define('APP_BASE', realpath(dirname(__FILE__)));
希望這會有所幫助,加油:)
您應該嘗試使用這樣的規則,而實際上它與config或.htaccess中的位置無關。 但是,如果使用apache配置,請確保它在Directory
指令中。 並重新啟動apache進行任何配置更改。
<Directory /var/path/to/root>
RewriteEngine On
RewriteBase /
# point all requests to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.