【问题标题】:Can't exclude directories from .htaccess mobile redirect?无法从 .htaccess 移动重定向中排除目录?
【发布时间】:2019-06-18 00:40:52
【问题描述】:

我正在使用 .htaccess 移动重定向,我在此处的较早帖子中找到了该重定向。它工作得很好,但我需要从重定向规则中排除几个目录。

我试过了:

RewriteRule !^your-directory($|/) http://www.example.com%{REQUEST_URI} [L,R=301]

杀死目录。

还有:

RewriteCond %{REQUEST_URI} "/your-directory/"

什么都不做。

我也尝试将它们放在不同的地方。

什么代码,我把它放在哪里。我只需要这些目录不受重写规则甚至整个根 .htaccess 文件的影响。

这就是我正在使用的。

# Set an environment variable for http/https.
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=ps:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=ps:http]

# Check if m=1 is set and set cookie 'm' equal to 1.
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$)
RewriteRule ^ - [CO=m:1:example.com]

# Check if m=0 is set and set cookie 'm' equal to 0.
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteRule ^ - [CO=m:0:example.com]

# Cookie can't be set and read in the same request so check.
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteRule ^ - [S=1]

# Check if this looks like a mobile device.
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} 
"android|blackberry|ipad|iphone|ipod|iemobile|opera 
mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile} !^$
# Check if we're not already on the mobile site.
RewriteCond %{HTTP_HOST} !^m\.
# Check if cookie is not set to force desktop site.
RewriteCond %{HTTP_COOKIE} !^.*m=0.*$ [NC]
# Now redirect to the mobile site preserving http or https.
RewriteRule ^ %{ENV:ps}://m.example.com%{REQUEST_URI} [R,L]

# Check if this looks like a desktop device.
RewriteCond %{HTTP_USER_AGENT} "! 
(android|blackberry|ipad|iphone|ipod|iemobile|opera 
mobile|palmos|webos|googlebot-mobile)" [NC]
# Check if we're on the mobile site.
RewriteCond %{HTTP_HOST} ^m\.
# Check if cookie is not set to force mobile site.
RewriteCond %{HTTP_COOKIE} !^.*m=1.*$ [NC]
# Now redirect to the mobile site preserving http or https.
RewriteRule ^ %{ENV:ps}://example.com%{REQUEST_URI} [R,L]

【问题讨论】:

  • 我有点 hack 和 slash 与 mod-rewrite,但试一试:RewriteRule ^(directory1|directory2)($|/) - [L]
  • 我应该把它放在哪里?我把它放在上面的代码之前和之后,它没有效果。目录仍然转到 m。
  • # Check if this looks like a mobile device. 行上方看起来是最好的地方,如果它不起作用,我会尝试你可以在谷歌上搜索的几百种变体之一
  • 在那里尝试过,同样的事情。那就是问题所在。找不到一个有效的。

标签: php .htaccess


【解决方案1】:

我想你的第二个 RewriteCond 差不多了。试试这样的:

RewriteCond %{REQUEST_URI} !^/?(directory1|directory2)($|/)

在 directory1|directory2|etc 中放置任意数量的目录。

这应该是“如果 REQUEST_URI 不以 /directory1 或 /directory2 开头”。

它将匹配:

  • /directory1/dasdas
  • /目录1
  • /directory1/

我认为这就是你想要的。

【讨论】:

  • 同样的事情。它仍然将目录重定向到 m.
  • 我怀疑您还有其他问题。也许规则正在另一个配置文件或另一个 htaccess 文件中应用?
  • 我刚刚测试了这个答案,将您的文件原样保存,将该行放在“# Check if this looks like a mobile device”的正上方。它完全按照需要工作。我正在使用 httpd 2.4.39。我建议从你的 htaccess 中退后一步,看看你的配置的其余部分。
猜你喜欢
  • 2018-04-26
  • 2022-11-27
  • 1970-01-01
  • 1970-01-01
  • 2021-04-27
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
相关资源
最近更新 更多