我已经绞尽脑汁好几个小时了,但似乎无法让它工作。请帮忙。我正在尝试让 apache 打开没有扩展名的 php 文件。这是我的 apache2.conf:
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride Indexes
Allow from All
Require all granted
</Directory>
<Directory /var/www/subdomain.example.com/public_html >
Options +MultiViews
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
IncludeOptional sites-enabled/*.conf
我在 sites-enabled(已禁用 000-default.conf)中获得了这两个配置文件..这是第一个,我们将其称为 example.com.conf:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
CustomLog ${APACHE_LOG_DIR}/exmple.com.access.log combined
</VirtualHost>
对于第二个subdomain.example.com.conf:
<VirtualHost *:80>
ServerName subdomain.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/subdomain.example.com/public_html
<Directory /var/www/subdomain.example.com/public_html >
allow from all
AllowOverride All
Options FollowSymLinks
Options +MultiViews
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME}\.php -f
# RewriteRule ^(.*)$ $1.php
</Directory>
ErrorLog ${APACHE_LOG_DIR}/subdomain.example.com.error.log
CustomLog ${APACHE_LOG_DIR}/subdomain.example.com.access.log combined
</VirtualHost>
有了这个:
- example.com/index-有效(虽然它是一个 html 文件)
- subdomain.example.com-有效(我获得了我的index.php服务)
- subdomian.example.com/index.php - 有效(我获得了我的 index.php 文件)
- subdomian.example.com/index——不起作用
所以我猜是我的 apache 配置文件出了问题。。。请帮忙。我做错了什么?
答案1
您需要“MultiviewsMatch Any”吗?这是因为文件扩展名实际上不是 txt/html/etc 那样的 mimetype 映射。
答案2
这是实现它的一种方法:
<Directory /var/www/>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ $1.htm
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
</Directory>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
CustomLog ${APACHE_LOG_DIR}/exmple.com.access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName subdomain.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/subdomain.example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/subdomain.example.com.error.log
CustomLog ${APACHE_LOG_DIR}/subdomain.example.com.access.log combined
</VirtualHost>
- 如果要应用所有虚拟主机,则需要将重写规则放在虚拟主机之外
#
是注释,因此禁用命令紧接着写入\.
是正则表达式中的转义点,外部则不需要
还可以简化一些多余的代码:
<Directory /var/www/subdomain.example.com/public_html >
Options +MultiViews
</Directory>
和
<Directory /var/www/subdomain.example.com/public_html >
allow from all
AllowOverride All
Options FollowSymLinks
Options +MultiViews
</Directory>
可以简单地写
<Directory /var/www/subdomain.example.com/public_html >
AllowOverride All
Options +FollowSymLinks +MultiViews
</Directory>
答案3
Froggiz 的答案只需要“RewriteEngine on”即可在我的配置中工作:
<Directory /var/www/>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ $1.htm
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
</Directory>