我有一个Apache虚拟主机configuration为WordPress的网站。
<VirtualHost *:80> ServerName 67.178.132.253 DocumentRoot /home/david/wordpressWebsite # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^index\.php$ - [L] RewriteCond /home/david/wordpressWebsite%{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress </VirtualHost>
我如何避免两次硬编码/home/david/wordpressWebsite ? 由于涉及额外的请求,我不想使用REQUEST_URI 。
这是我使用目录上下文的尝试。 我在网站上提供了这些内容的文件。
DocumentRoot /home/david/wordpressWebsite <Directory /home/david/wordpressWebsite> RewriteEngine On RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L] # You need these somewhere, anyway; better to not put them on the root. Order allow,deny Allow from all </Directory>
出于某种原因,Apache在/ var / www中查找我的错误日志中显示的文件:
[error] [client 69.175.67.64] File does not exist: /var/www/about, referer: http://67.178.132.253/
对于那些偶然发现并想要回答原始问题的人来说:
我如何避免在这个Apache虚拟主机中重复DocumentRoot?
DocumentRoot指令的值可以通过使用服务器variables来访问
%{DOCUMENT_ROOT} 。 这意味着在OP的例子中,而不是写作:
RewriteCond /home/david/wordpressWebsite%{REQUEST_FILENAME} !-f
我们可以写
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
因此,唯一的地方你应该硬编码你的根文件path在DocumentRoot指令。
这些Wordpress规则被build立在一个<Directory>上下文中,正如他们正在寻找没有前导斜杠的^index\.php$ ; 该规则将永远不会在<VirtualHost>上下文中匹配。
一旦在<Directory>上下文中, REQUEST_FILENAMEvariables包含物理文件位置,如文档中的解释:
如果在引用REQUEST_FILENAME时服务器已经确定了该文件或脚本的完整本地文件系统path, 否则,如在虚拟主机上下文中使用时,与REQUEST_URI的值相同。
所以,尝试这样的事情:
<Directory /home/david/wordpressWebsite> RewriteEngine On RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L] # You need these somewhere, anyway; better to not put them on the root. Order allow,deny Allow from all </Directory>
至于硬编码两次(在DocumentRoot和<Directory>设置中):如果由于某种原因确实需要避免,请查看Apache 2.4的“configuration文件variables”function 。