【问题标题】:Redirect all url using .htaccess except one dynamic url使用 .htaccess 重定向所有 url,除了一个动态 url
【发布时间】:2014-12-08 10:27:14
【问题描述】:

我想将所有 url 从 http 重定向到 https,这对我来说很好。

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

现在我想为这条规则添加一个例外,即每当用户请求以下动态 url(最后一个字符串是动态生成的)时,它不应该强制 https 重定向。

sub.domian.dev/api/v/1/download/zip/token/8397298347ksjdnkjasdn0394834 

我尝试了这条不起作用的规则。

#url to exclude
RewriteCond %{REQUEST_URI} !^/api/v/1$

谁能给我指点一下如何处理这个?

谢谢。

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    您可以使用此代码:

    RewriteEngine On
    
    # except /api/v/1/ requests redirect everything else to https
    RewriteCond %{THE_REQUEST} !\s/+api/v/1/ [NC]
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    RewriteRule ^ index.php [L]
    

    【讨论】:

    • 谢谢,我不希望/api/v/1/ 强制http,用户应该能够使用httphttps 访问此URL。
    【解决方案2】:

    你可以使用:

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} !^/api/v/1/
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 2015-10-01
      • 2021-06-30
      • 2016-09-18
      相关资源
      最近更新 更多