我有一个网址https://facebook.****.in,设置为指向目录 /var/www/html/facebook_app。其虚拟主机文件是 /etc/apache2/sites-available/facebook.****.in.conf。
我想向 URL 添加一个指向另一个自定义目录的路径,这样,
https://facebook.****.in/apis -> /var/www/html/apis
我搜索了这个论坛并找到了一些解决方案这里
但我不确定应该将别名添加到哪里?应该是 default.conf 吗?还是 facebook.****.in.conf 文件。
如果是 facebook.****.in.conf 文件,我应该如何指定别名?我想将其托管在 SSL 的 443 端口上。
facebook.****.in.conf 文件:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName facebook.****.in
ServerAlias facebook.****.in
DocumentRoot /var/www/html/facebook_app/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster@localhost
ServerName facebook.****.in
ServerAlias facebook.****.in
DocumentRoot /var/www/html/facebook_app/
# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on
# A self-signed (snakeoil) certificate can be created by installing
# the ssl-cert package. See
# /usr/share/doc/apache2.2-common/README.Debian.gz for more info.
# If both key and certificate are stored in the same file, only the
# SSLCertificateFile directive is needed.
SSLCertificateFile /etc/apache2/ssl/facebook_****_in.crt
SSLCertificateKeyFile /etc/apache2/ssl/facebook.****.in.key
SSLCertificateChainFile /etc/apache2/ssl/facebook.****.in.ca-bundle
</VirtualHost>
这是.htaccess 文件。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
我有一个 index.php 文件,它将 url 参数映射到类和方法。
答案1
您必须在配置文件的两个部分内添加 ApacheAlias
和Directory
指令,以便启用 HTTP 和 HTTPS 连接。VirtualHost
facebook.****.in.conf
您应该添加的配置片段必须是这样的
Alias "/apis" "/var/www/html/apis"
# Now set directory permission
<Directory "/var/www/html/apis">
AllowOverride None
Options -Indexes +FollowSymLinks
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>
我认为您的 Apache Server 版本高于 2.3,因此仅添加该Require all granted
语句就可以了,而无需检查版本。不过我还是添加了它以防万一。
此外,我还出于安全和性能目的添加了一些其他指令。下面是它们的简要概述
AllowOverride None
:出于安全和性能考虑,它禁用将.htaccess
文件放入目录中。Apache 软件基金会建议在http://httpd.apache.org/docs/current/mod/core.html#allowoverride
“出于安全和性能原因,请勿将块中的 AllowOverride 设置为 None 以外的任何值。相反,请查找(或创建)引用您实际计划放置 .htaccess 文件的目录的块。”
Options -Indexes +FollowSymLinks
:当所需 URL 映射到没有 a 的目录(例如 index.html 文件)时,该-Indexes
选项将阻止 Web 服务器(具体为模块)返回目录的格式化列表。除此之外,该选项还允许服务器跟踪此目录中的符号链接。您可以在 Apache Web 服务器官方文档中获取有关该语句和其他可用选项的更多信息mod_autoindex
DirectoryIndex
+FollowSymLinks
Options
http://httpd.apache.org/docs/current/mod/core.html#options
/var/www/html/apis
还要记得为运行 Apache 服务器的用户(在 Ubuntu 中默认)设置目录和文件的正确权限www-data
,并重新启动 Apache 服务以使更改生效。
sudo service apache2 restart
更新
如果该目录中没有文件(例如 index.php),您也可以将DirectoryIndex
指令包含到块中。Directory
index.html
DirectoryIndex index.php
也可以指定多个文件。在这种情况下,将返回找到的第一个文件
DirectoryIndex index.html index.php
更多详细信息请访问http://httpd.apache.org/docs/current/es/mod/mod_dir.html#directoryindex