【问题标题】:redirection via ht access file is not working properly通过 htaccess 文件重定向无法正常工作
【发布时间】:2017-04-06 13:27:58
【问题描述】:

这是场景:

有一个名为 "example.com" 的网站和一个名为 "mobile" 的文件夹,用于通过手机或标签访问网站

如果用户在浏览器中仅键入 "example.com",则应将其重定向到 https://www.example.com,它按照 .htaccess 中的以下代码工作:

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

或者,如果用户从移动设备访问 url https://www.example.com,那么它应该被重定向到 https://mobile.example.com

现在,每当我从手机访问网址 https://www.example.com 它将浏览器重定向到https://mobile.example.com,但再次根据 .htaccess 中的代码,它考虑 url "example.com" 的这个特定部分并将浏览器重定向回https://www.example.com,这样它最终结束了无限循环生成错误:

“获取页面超过了 10 次重定向的最大值。”

我尝试在 .htaccess 文件中放入类似的内容,以免重定向到 https://www.example.com,以防有一个 "mobile" 词作为前缀但它不起作用

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

请指教。

这就是 .htaccess 文件的构造方式

RewriteEngine on

RewriteRule ^mobile - [L]

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

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

##DISABLE VIEWING DIRECTORIES##
Options -Indexes
##DISABLE VIEWING DIRECTORIES##

##DISABLE VIEWING DIRECTORIES FILES##
RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^https://(www\.)?example.com [NC]
RewriteCond %{HTTP_REFERER} !^https://(www\.)?example.com/.*$ [NC]
RewriteRule .*\.(png|gif)$ - [F,NC]
##DISABLE VIEWING DIRECTORIES FILES##

##DISABLE VIEWING admin DIRECTORY FILES##
RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^https://(www\.)?example.com [NC]
RewriteCond %{HTTP_REFERER} !^https://(www\.)?example.com/.*$ [NC]
RewriteRule ^admin/.+\.php$ - [F,L]
##DISABLE VIEWING admin DIRECTORY FILES##

header.php 中的重定向代码

<script type="text/javascript">
if (screen.width <= 699) {
document.location = "https://mobile.example.com";
}
</script>

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    您没有正确使用 RewriteCond。如果您想在 uri 为 /mobile/ 时跳过 url 重写并将 example.com 重定向到 https://www.example.com 您可以使用以下

    #Skip url rewriting if requested uri is /mobile
    RewriteRule ^mobile - [L]
    #redirect example.com/ to http://www.example.com/
    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteRule ^/?$ https://www.example.com/ [L,R]
    

    / 不是 mod-rewrite 中的特殊字符,不需要转义。

    【讨论】:

    • RewriteRule ^mobile - [L] RewriteCond %{HTTP_HOST} ^example\.com$ RewriteRule ^/?$ "https\:\/\/www\.example\.com" R=301 ,L] 但它不起作用重定向仍然存在,我在做什么错了
    • @team 清除浏览器缓存
    • 全部完成...但它仍然有同样的问题。我已经编辑了我的问题并添加了完整的 .htaccess 文件,因为我不确定,我在哪里做错了跨度>
    • 还有什么建议吗??
    • 您编辑的 htaccess 在我的服务器上按预期工作。你能解释清楚你的问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2013-11-04
    • 2023-04-02
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多