donglian4770 2016-12-10 09:36
浏览 64

如何重定向到特定的URL

I would like to redirect if user open with

1 http://example.com to https://www.example.com

2 http://www.example.com to https://www.example.com

3 http://example.com/user/(anystring) to https://www.example.com/user/(anystring)

4 http://www.example.com/user/(anystring) to https://www.example.com/user/(anystring)

I've tried this htaccess

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com\/user\/*$ [OR] 
RewriteCond %{HTTP_HOST} ^www\.example\.com\/user\/*$
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/user\/$1" [R=301,L]

3rd and 4th is not working properly, (anystring) is not accessible, I'm going to set COOKIE, but its not available after redirection.

  • 写回答

1条回答 默认 最新

  • dongyushen9063 2016-12-10 09:43
    关注

    I think you can simplify this to a single rule:

    RewriteEngine on
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
    RewriteRule ^/?(.*)$ https://www.example.com/$1 [R=301,L,QSA]
    

    And the test for the host name is actually only required if you have other virtual hosts based in the same rule set that you do not want to rewrite.


    And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

    评论

报告相同问题?

悬赏问题

  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败