繁体   English   中英

.htaccess 重定向不重定向

[英].htaccess redirect not redirecting

我正在尝试使用 .htaccess 将页面重定向到同一网站上的新位置物理文件名是 displayitems.php 但 .htaccess 中有一条规则

RewriteRule ^buy-online-(.*) ./displayitems.php?url=$1

这是为了处理用户朋友的 URL 并且效果很好。

现在我想将这些用户友好的 url 重定向到同一网站上的新位置,例如。

redirect https://example.com/buy-online-alhabib-rings4-sku-1658906163 https://example.com/products/jewelry/buy-online-alhabib-rings4-sku-1658906163 [R=301]

redirect https://example.com/buy-online-alhabib-rings3-sku-1658906162 https://example.com/products/jewelry/buy-online-alhabib-rings3-sku-1658906162 [R=301]

redirect https://example.com/buy-online-alhabib-rings2-sku-1658906161 https://example.com/products/jewelry/buy-online-alhabib-rings2-sku-1658906161 [R=301]

redirect https://example.com/buy-online-alhabib-rings1-sku-1658906160 https://example.com/products/jewelry/buy-online-alhabib-rings1-sku-1658906160 [R=301]

这些用户友好的 url 没有任何扩展名,如“.php”“.htm”等

但什么也没发生。

我在 php 文件中添加了这段代码,以检查 url 是否不包含 \products\ 而不是将其重定向到具有相同名称的新位置,为了测试,我只是用 302 重定向它,一旦所有测试我都会将其更改为 301

if (strpos($_SERVER['REQUEST_URI'], "/products/") === false) { $NewAddress = strtolower("Location:". $ini['website_address_https']. "products/".$Product['categoriesname']."/".$Product['BrandName'].$_SERVER['REQUEST_URI']); header("$NewAddress",TRUE,302); }

redirect https://example.com/buy-online-alhabib-rings4-sku-1658906163 https://example.com/products/jewelry/buy-online-alhabib-rings4-sku-1658906163 [R=301]

这里有3个主要问题:

  1. mod_alias Redirect指令采用相对于根的 URL 路径(以斜杠开头)作为源 URL,而不是绝对的 URL。因此,以上内容永远不会匹配。

  2. 您使用了 mod_rewrite 的混合语法。 [R=301]RewriteRule (mod_rewrite)标志参数,与 mod_alias Redirect指令无关。 Redirect将 HTTP 状态代码作为可选的第二个参数。 例如。 Redirect 301 /buy-online-alhabib-rings4-sku-1658906163...

  3. 由于您正在使用 mod_rewrite (即RewriteRule )进行内部重写,因此您也应该将 mod_rewrite 用于外部重定向以避免潜在的冲突。 然后,在您的内部重写之前,这些重定向需要 go。

此外,

  1. 在您发布的 4 个重定向中,您似乎只是在 URL 路径的开头注入/products/jewelry 这不需要 4 个单独的规则,前提是您要重定向所有遵循此特定格式的 URL。

请尝试以下操作:

RewriteEngine On

# Inject (redirect) "/product/jewelry" at the start of the URL-path
RewriteRule ^buy-online-alhabib-rings\d-sku-\d+$ /products/jewelry/$0 [R=301,L]

# Internal rewrite
RewriteRule ^buy-online-(.*) displayitems.php?url=$1 [L]

第一条规则中的$0反向引用包含与RewriteRule模式匹配的整个 URL 路径。

请注意,我还从最后一条规则中替换字符串的开头删除了./ 这在这里是不必要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM