在`.htaccess`文件中根据条件重定向

huangapple go评论45阅读模式
英文:

Redirect on condition in htaccess file

问题

我正在尝试在Apache的.htaccess中使用If块进行一些重定向,只有在满足条件时才会执行。我有以下配置:

<If "%{HTTP_HOST} == 'shoppingmall-local.ventajon.com:8433'">
  # 开始
  RedirectMatch 301 ^/tiendas-online$ https://shoppingmall-local.ventajon.com/tiendas-online

  # 购物车
  Redirect 301 /tiendas-online/cart https://shoppingmall-local.ventajon.com/tiendas-online/cart

  # 账户
  Redirect 301 /tiendas-online/cuenta https://shoppingmall-local.ventajon.com/tiendas-online/cuenta
</If>

但它不起作用。当我点击链接时,它不会重定向。希望你能帮助我。提前感谢。

英文:

I'm trying to make some redirects on the one condition, using If block in Apache, in the .htaccess of my project. I have this:

&lt;If &quot;%{HTTP_HOST} == &#39;shoppingmall-local.ventajon.com:8433&#39;&quot;&gt;
  # inicio
  RedirectMatch 301 ^/tiendas-online$ https://shoppingmall-local.ventajon.com/tiendas-online

  # carrito de compras
  Redirect 301 /tiendas-online/cart https://shoppingmall-local.ventajon.com/tiendas-online/cart

# cuenta
  Redirect 301 /tiendas-online/cuenta https://shoppingmall-local.ventajon.com/tiendas-online/cuenta
&lt;/If&gt;

But it doesn't work. When I click in the link, it doesn't redirect. I hope you can help me. Thanks in advance.

答案1

得分: 1

以下是您要翻译的内容:

"你没有说明您正在请求的URL(当您“点击链接”时)或(重要的是)配置文件中的其他指令以及这些指令的相应放置位置。

孤立地看,您发布的规则块实际上没有任何问题(所以很可能与其他指令冲突),但是,这些规则可以使用mod_rewrite来简化(您肯定已经在用它来定位这些请求)。

请尝试在根目录的顶部.htaccess文件中使用以下内容:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(shoppingmall-local\.ventajon\.com):8433$
RewriteRule ^tiendas-online(/cart|/cuenta)?$ https://%1%{REQUEST_URI} [R=301,L]

这本质上与您现有的规则块执行相同。(除了它不会像Redirect指令那样重定向/tiendas-online/cart/<something>/tiendas-online/cuenta/<something>。)此规则_必须_在.htaccess文件的顶部附近放置,位于任何现有的mod_rewrite指令之前。

我假设您的源URL不包含尾随斜杠。

(如果已经在配置文件中的后面出现了RewriteEngine指令,则无需重复它。)

我在注释中提到了这一点(似乎已经得到了确认),但是,8433看起来像是拼写错误。更常见的是将8443用作HTTPS的替代端口,而不是您在这里写的8433

在进行测试之前,您应该首先使用302(临时)重定向以避免潜在的缓存问题,并确保清除浏览器(和任何中介)缓存后再进行测试。

如果您只有一个_主机名_(即shoppingmall-local\.ventajon\.com),则可以进一步简化该规则,因为您只需检查端口号即可。例如:

RewriteCond %{SERVER_PORT} =8433
RewriteRule ^tiendas-online(/cart|/cuenta)?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]"
英文:

You've not stated the URL you are requesting (when you "click in the link") or (importantly) what other directives you have in the config file (and the corresponding placement of these directives).

In isolation there's nothing actually wrong with the rule block you've posted (so it's likely there is a conflict with other directives), however, these rules can be simplified using mod_rewrite (which you are most certainly using already to root these requests).

Try the following instead at the top of the root .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(shoppingmall-local\.ventajon\.com):8433$
RewriteRule ^tiendas-online(/cart|/cuenta)?$ https://%1%{REQUEST_URI} [R=301,L]

This essentially does the same as your existing rule block. (Except that it won't redirect /tiendas-online/cart/&lt;something&gt; and /tiendas-online/cuenta/&lt;something&gt; as the Redirect directives would.) This rule must go near the top of the .htaccess file before any existing mod_rewrite directives.

I'm assuming your source URLs do not contain a trailing slash.

(You do not need to repeat the RewriteEngine directive if is already occurs later in the config file.)

I mentioned this in comments (and this was seemingly confirmed), however, 8433 does look-like a typo. It would be more common to be using 8443 as an alternative port for HTTPS, not 8433 as you have written here?

You should test first with a 302 (temporary) redirect to avoid potential caching issues and you should ensure the browser (and any intermediary) cache is cleared before testing.

If you only have one hostname (ie. shoppingmall-local\.ventajon\.com) then the rule can be simplified further since you can just check for the port number instead. For example:

RewriteCond %{SERVER_PORT} =8433
RewriteRule ^tiendas-online(/cart|/cuenta)?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

huangapple
  • 本文由 发表于 2023年4月17日 17:34:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定