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