【问题标题】:.htaccess: Subdomains, Rewrite, SSL and password protection.htaccess:子域、重写、SSL 和密码保护
【发布时间】:2013-12-20 16:25:57
【问题描述】:

一段时间以来,我一直在努力解决这个问题,但没有找到满足我所有需求的解决方案。 我有两个站点,example.com 和 example.de,我希望每个访问者始终被重定向到 www.example.com 此外,所有 url 都必须转换为相应的 www.example.com URL。一些子文件夹受密码保护。 最重要的是,我为 example.com 使用 SSL 证书

所以在几行中我希望 Apache 如下所示:

Visitor types this URL -> Apache redirects to (301)
http://example.de -> https://www.example.com
http://www.example.de -> https://www.example.com
http://example.com -> https://www.example.com
http://example.de/path/to/some/file.php -> https://www.example.com/path/to/some/file.php
http://example.de/stats (Login required) -> https://www.example.com/stats (Login required)

我尝试了几种 .htaccess 配置。我最好的结果设法重定向了前四个案例:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]`

但是,我无法通过重定向访问受密码保护的文件夹。我的浏览器显示 401 错误消息(因为身份验证模块在重写模块之前执行)。有人有想法吗?

编辑: 这似乎是解决方案。我还在测试,但看起来还可以。

/.htaccess

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteRule ^(.*)$ - [E=REWRITTEN:1]

/stats/.htaccess

AuthType Basic
AuthName "Members Only"
AuthUserFile /home/.htpasswd

Order Deny,Allow
Satisfy any
Deny from all
Require valid-user
Allow from env=!REWRITTEN

【问题讨论】:

    标签: apache .htaccess mod-rewrite ssl


    【解决方案1】:

    试试:

    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^example\.(com|de)$ [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
    

    【讨论】:

    • @Jon 唯一的办法就是这样做:stackoverflow.com/questions/7334307/…
    • 谢谢,您的四行似乎与我的问题中的五行完全一样。所以,登录问题仍然存在。我会阅读另一个线程。
    • 不幸的是,另一个线程没有帮助。我认为问题在于 Web 根目录(RewriteRules 所在的位置)中的 .htaccess 文件优先于 /stats/.htaccess,这会扰乱身份验证过程。
    • @user3123203 您需要为 not www.example.com 的主机排除 AUTH(如答案),这样,在重定向。
    • 谢谢。我没有想到这一点,因为我不想弄乱密码保护。 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多