【问题标题】:htaccess rewrite to https except for few pages and fileshtaccess 重写为 https,除了少数页面和文件
【发布时间】:2019-04-14 16:15:53
【问题描述】:

我使用 htaccess 文件来重写一些 url,并将 http 请求重写为 https:

# No Caching for page files
<filesMatch "\.(html|htm|js|css|php)$">
<IfModule mod_headers.c>
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0
</IfModule>
</filesMatch>

# Error pages
ErrorDocument 404 https://%{HTTP_HOST}/#/404
ErrorDocument 500 https://%{HTTP_HOST}/#/500

RewriteEngine On

# Rewrite www to non
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R,L]

# Rewrite http to https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^\/tvgids\/
RewriteRule ^/?(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

# Ignore conditions for files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteBase /

# Rewrite /api to api.php
RewriteRule ^api/(.+)$ api/api.php [QSA,L]

# Rewrite /tvgids to guide/index.php
RewriteRule ^tvgids/(.+)$ guide/index.php [QSA,L]

# Rewrite /tv to m3u/index.php
RewriteRule ^tv/(.+)$ m3u/index.php [QSA,L]

# Rewrite /get to get/index.php
RewriteRule ^get/(.+)$ get/index.php

# Rewrite /downloads to downloads.php
# RewriteRule ^download/(.+)$ download/download.php [QSA,L]

但是 /tvgids 的重写条件不起作用:

RewriteCond %{REQUEST_URI} !^\/tvgids\/

如果我尝试输入 /tvgids/abc 我会得到 404。

我需要将所有页面重写为 https,除了少数页面,因为它们用于不支持 https 的 android 上的应用程序。

谁能帮我解决这个问题 - 现在我必须为所有页面禁用 ssl,否则没有任何效果。我看不出我做错了什么,是条件设置不正确吗?

我也试过这个:

RewriteCond %{REQUEST_URI} !(tvgids\/.*|tv\/.*|get\/.*|\.png|\.jpg|\.jpeg|\.ico)$ [NC]

这也将我重定向到 https.. 但如果我在这里测试它: https://htaccess.madewithlove.be/

它确实有效——所以我在这里做错了吗?

【问题讨论】:

    标签: php apache .htaccess routes


    【解决方案1】:

    我实际上已经使用此代码:

    # Rewrite http to https
    RewriteCond %{HTTPS} !=on
    RewriteCond %{THE_REQUEST} !(/tvgids/|/tv/) [NC]
    RewriteRule ^/?(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    

    RewriteCond %{THE_REQUEST} !/tvgids/ [NC] 设置了一个条件,当 tvgids/ 是请求时,不会重定向到 https。而且我不需要排除图像文件类型,因为我已经设置了排除文件夹和文件的条件..

    【讨论】:

      猜你喜欢
      • 2016-11-08
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      相关资源
      最近更新 更多