英文:
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:
<If "%{HTTP_HOST} == 'shoppingmall-local.ventajon.com:8433'">
# 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
</If>
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/<something>
and /tiendas-online/cuenta/<something>
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论