SCRIPT_NAME and PHP_SELF with mod_rewrite in .conf

孤街醉人 提交于 2019-12-13 04:31:39

问题


After moving this mod_rewrite from .htaccess to apache2.conf both $_SERVER['SCRIPT_NAME'] and $_SERVER['PHP_SELF'] returns different values than else..

#   point all requests to index.php
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^                   /index.php [L,NS]

example

URL:

http://domain.com/path/to/dir/

If the rewrite is in .htaccess then $_SERVER['PHP_SELF'] returns /index.php

If the rewrite is in apache2.conf then $_SERVER['PHP_SELF'] returns /path/to/dir/

How to get the relative path to doc root when $_SERVER['PHP_SELF'] doesn't work?


回答1:


To get the relative path to document root you could use $_SERVER['DOCUMENT_ROOT'] eg usage:

define('APP_BASE', $_SERVER['DOCUMENT_ROOT']);

or you could also use realpath(dirname(__FILE__)) like so

define('APP_BASE', realpath(dirname(__FILE__)));

Hope this helps, cheers :)




回答2:


You should try using your rule like this and it really shouldn't matter where it's located as in the config or .htaccess. However make sure it's in a Directory directive if using the apache config. And restart apache for any config changes.

<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>


来源:https://stackoverflow.com/questions/30377363/script-name-and-php-self-with-mod-rewrite-in-conf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!