【发布时间】:2016-12-30 06:47:56
【问题描述】:
当我运行 api.troop37bsa.org/user 时,我可以访问该页面,但是当我运行 https://api.troop37bsa.org/user 时,我收到 404 错误。当我运行 api.troop37bsa.org/?url=user 或安全版本时,我得到了正确的页面。
我正在使用 .htaccess 文件来重写 url,以便子域之后的所有内容都被视为参数字符串。
所以我的问题有两个方面。我怎样才能让https://api.troop37bsa.org/user 工作,以及我可以做些什么来减小 .conf 文件的大小并减少或合并我的 .htaccess 文件。
我有一个 Apache 服务器 2.4.18。
我的 .conf 文件设置如下:
<VirtualHost *:80>
ServerAdmin kreichner@troop37bsa.org
ServerName api.troop37bsa.org
ServerAlias api.troop37bsa.org
DocumentRoot /var/www/troop37bsa.org/api/public
<Directory /var/www/troop37bsa.org/api/public>
DirectoryIndex index.php index.html
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
AccessFileName .htaccess
ErrorLog /var/www/troop37bsa.org/api/public/apache_error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#RewriteEngine on
#RewriteCond %{SERVER_NAME} =api.troop37bsa.org
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin kreichner@troop37bsa.org
ServerName api.troop37bsa.org
ServerAlias api.troop37bsa.org
DocumentRoot /var/www/troop37bsa.org/api/public
<Directory /var/www/troop37bsa.org/api/public>
DirectoryIndex index.php index.html
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
AccessFileName .htaccess
ErrorLog /var/www/troop37bsa.org/api/public/apache_https_error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
在我的子域文件夹中(即 /var/www/troop37bsa.org/api/public)我有一个 .htaccess 文件
ReWriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^(.+)$ %2index.php?url=$1 [QSA,L]
我还使用了 Letsencrypt SSL 证书,否则它似乎工作正常。
谢谢
【问题讨论】:
-
您是否尝试过使用 AllowOverride none 完全禁用 .htaccess(因为您似乎可以访问虚拟主机但无论如何都不需要它),只需在 VirtualHost 配置中添加一个简单的“FallBackResource /index.php”对于两个虚拟主机?如果您坚持使用该组重写,则最后一个条件可以避免任何正常的 uri 以您似乎想要的方式结束 index.php,这就是 /user 如果它不存在会给您一个 404 的原因。
-
@ezra-s 谢谢。这就是我想要的。主要的困难似乎是一个我以前没有见过的 .conf 文件。它显然是由letsencrypt生成的。无论如何,它指向 /publicl 而不是 publc。我删除了 l,并删除了虚拟主机 *443,整个站点按预期工作。
-
很高兴你解决了。
标签: apache .htaccess mod-rewrite