Flask忽略.htaccess文件。

huangapple go评论65阅读模式
英文:

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
&lt;Directory /var/www/html&gt;
    WSGIProcessGroup main_proc
    WSGIApplicationGroup %{GLOBAL}
    AllowOverride All

    AuthType shibboleth                                                                                                                                                                                                                
    ShibRequestSetting requireSession 1                                                                                                                                                                                                
    Require shib-session                                                                                                                                                                                                               
&lt;/Directory&gt;

答案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.

&lt;Directory /var/www/flask_app&gt;
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
&lt;/Directory&gt; 

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 &lt;IfModule&gt; 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.

huangapple
  • 本文由 发表于 2023年2月27日 11:55:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定