doufuxi7093 2016-02-19 03:19
浏览 39

多个htaccess重定向,http到https但页面除外

i use mod_rewrite to allow "friendly URLS" to replace search parameters, which works fine. However i need to now redirect all traffic to https:// but allow only some pages to remain on http:// (i need to do this so my referrals are collected by the sites i send traffic to)

Over the years i have added to the .htaccess file, most works OK but i dont fully understand it, so it may be getting messy too :-(

i have the below (i have cut out anything i dont think is relevant)

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
</IfModule>

<IfModule mod_rewrite.c>
  RewriteCond %{SCRIPT_FILENAME} -d [OR]
  RewriteCond %{SCRIPT_FILENAME} -f
  RewriteRule "(^|/)\." - [F]
</IfModule>

<IfModule mod_rewrite.c>

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^profile/([A-Za-z0-9'/-]+)-([0-9]+)$ profile.php?id=$2 [NC,L,QSA]
# NOTE: this allows for profile.php?id=123 to be replaced with /profile/name-123

# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

</IfModule>

The above works OK, but redirect ALL pages to https

I have tried adding /changing the https redirect part to the below to allow urlout.php not to be redirected, but this just loops and i get a browser warning that the page has too many redirects :-(

# redirects any http:// traffic to https://
RewriteCond %{HTTPS} !on
RewriteCond ${REQUEST_URI} !^/urlout\.php
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}  [R=301,L]

I have tried a few other things too, but they either dont redirect and everything goes to https, or i get a loop error or internal 500 error...

Basically i want all pages to go to https, but not urlout.php

Any advice on any of the code above would me much appreciated!

  • 写回答

2条回答 默认 最新

  • dps57553 2016-02-19 03:27
    关注

    If you want to redirect some spacific pages to https, forexample, To redirect :

    to

    And

    to - https://www.example.com/page2.html

    You may try the following :

     RewriteEngine on
    
     RewriteCond %{HTTPS} off
     RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]
    

    If you want to redirect the whole site from http to https, you can use the following :

     RewriteEngine on
    
    
    
     RewriteCond %{HTTPS} off
     RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]
    

    RewriteCond %{HTTPS} off is important to avoid redirect loop as it skips the rule for https requests.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败