【问题标题】:htaccess -> need to redirect all www to non-www and one page only to sslhtaccess -> 需要将所有 www 重定向到非 www 并且仅将一页重定向到 ssl
【发布时间】:2011-08-13 12:57:09
【问题描述】:

我“到处”都看过,但似乎无法弄清楚这一点。

我需要 htaccess 为我做两件事:

  1. 将所有 www 请求强制为非 www 请求
  2. 还要在所有页面上强制使用 http(非 ssl),但在这种情况下我需要 ssl

另外,这是一个使用永久链接的 wordpress 网站,所以我有那块 htaccess 代码 wordpress 也放在那里。但不知何故,一切正常,除了当我转到需要强制 ssl 的页面时,它只是重定向到站点的根目录。

这是我的代码的样子:

重写引擎开启 重写基础/

# Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301]

# Turn SSL on for secure-page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^secure-page[/]?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Turn SSL off everything but secure-page
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^secure-page[/]?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

所以结果是:

  1. 任何 www 和非 www 和 http url 都被重定向到非 www 和 http -> OK
  2. 任何 www 和非 www 和 https url(不包括安全页面)都被重定向到非 www 和 http -> OK
  3. secure-page url 被重定向到站点根目录,我不知道为什么 -> 不行

有人可以帮忙吗? 谢谢

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:
    RewriteEngine On
    # Redirect from www to non-www
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
    
    
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
    RewriteRule ^(.*)$  http://example.com/$1 [L,R=301]
    
    
    
    # Turn SSL on for secure-page
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    
    # Turn SSL off everything but secure-page
    RewriteCond %{HTTPS} on
    RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
    RewriteRule ^(.*)$  http://%{HTTP_HOST}/$1 [R=301,L]
    

    我已经更新了我的答案!

    【讨论】:

    • 嗨阿斯曼,感谢您的回复。但是,即使您要求更新,如果我转到 example.com/secure-page ,它仍然只是重定向到 example.com
    • @Ettiene 我编辑了我的答案!我在 test.hkh.com 上测试过,然后根据您的需要更改值!所以我认为它会好的!
    猜你喜欢
    • 2018-09-11
    • 1970-01-01
    • 2017-01-12
    • 2012-11-16
    • 2012-03-08
    • 2010-12-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    相关资源
    最近更新 更多