英文:
Flask ignores .htaccess file
问题
在我的Flask应用程序中提供服务的页面应该通过.htaccess文件限制访问。我知道我已经正确配置了Apache和.htaccess文件,因为对于相同目录中的.php文件正常工作。Flask(或mod-wsgi)需要以特定方式配置以使用.htaccess吗?
在我的.conf文件中,我有以下内容:
WSGIDaemonProcess main_proc processes=8 python-home=/var/www/html/venv
WSGIScriptAlias / /var/www/html/wsgi.py
<Directory /var/www/html>
WSGIProcessGroup main_proc
WSGIApplicationGroup %{GLOBAL}
AllowOverride All
AuthType shibboleth
ShibRequestSetting requireSession 1
Require shib-session
</Directory>
英文:
I have a page being served by my Flask app that is supposed to have access restricted via an .htaccess file. I know that I have Apache and the .htaccess file configured correctly because it's working correctly for a .php file in the same directory. Does Flask (or mod-wsgi) need to be configured a certain way to use .htaccess?
In my .conf
file, I have the following
WSGIDaemonProcess main_proc processes=8 python-home=/var/www/html/venv
WSGIScriptAlias / /var/www/html/wsgi.py
<Directory /var/www/html>
WSGIProcessGroup main_proc
WSGIApplicationGroup %{GLOBAL}
AllowOverride All
AuthType shibboleth
ShibRequestSetting requireSession 1
Require shib-session
</Directory>
答案1
得分: 3
不使用目录中的.htaccess
文件,您可以将相同的代码插入到Apache配置中。首先,您需要通过覆盖Directory
指令来启用.htaccess
。
<Directory /var/www/flask_app>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
然后启用重写模块并重新启动Apache,
sudo a2enmod rewrite
sudo service apache2 restart
您可以将.htaccess
代码添加到Apache配置文件中。
RewriteEngine On
#(需要Apache 2.4.8+)
RewriteOptions InheritDown
RewriteRule ^/index\.html$ - [L]
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteRule ^/. /index.html [L]
请注意,这里不需要使用<IfModule>
包装。而且RewriteBase
在服务器上下文中无效。我们需要使用前瞻(LA-U:)
来检索请求映射的文件名。
英文:
Instead of using the .htaccess
file in the directory, you can insert the same code in the apache configuration. First, you'll need to enable the htaccess by overriding the Directory directive.
<Directory /var/www/flask_app>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Then enabling the rewrite mod and restarting the apache,
sudo a2enmod rewrite
sudo service apache2 restart
You can add the htaccess code in apache configuration file.
RewriteEngine On
# (Requires Apache 2.4.8+)
RewriteOptions InheritDown
RewriteRule ^/index\.html$ - [L]
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteRule ^/. /index.html [L]
Notice here, the <IfModule>
wrapper is not required. And RewriteBase
is not valid in the server context. We'll need to use lookahead (LA-U:)
to retrieve the filename the request is mapped to.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论