.htaccess 使用RegExp和Mod Rewrite重定向URL

.htaccess 使用RegExp和Mod Rewrite重定向URL,.htaccess,magento,redirect,mod-rewrite,.htaccess,Magento,Redirect,Mod Rewrite,将Magento升级到1.9.3.4时,它重写了.htaccess文件,现在我只剩下2000多个手动创建的断开链接 由于旧的产品URL在末尾有一个随机数,而现在它们没有,所以我正在寻找一种重定向它们的方法 旧URL结构: http://example.com/category-1/subcategory/product-name-1421.html http://example.com/category-2/subcategory/product-name-5421.html http://ex

将Magento升级到1.9.3.4时,它重写了.htaccess文件,现在我只剩下2000多个手动创建的断开链接

由于旧的产品URL在末尾有一个随机数,而现在它们没有,所以我正在寻找一种重定向它们的方法

旧URL结构:

http://example.com/category-1/subcategory/product-name-1421.html
http://example.com/category-2/subcategory/product-name-5421.html
http://example.com/category-3/subcategory/product-name-5891.html
http://example.com/category-1/subcategory/product-name.html
http://example.com/category-2/subcategory/product-name.html
http://example.com/category-3/subcategory/product-name.html
新的URL结构:

http://example.com/category-1/subcategory/product-name-1421.html
http://example.com/category-2/subcategory/product-name-5421.html
http://example.com/category-3/subcategory/product-name-5891.html
http://example.com/category-1/subcategory/product-name.html
http://example.com/category-2/subcategory/product-name.html
http://example.com/category-3/subcategory/product-name.html

我知道我可以使用RegExp,比如
RewriteRule^category/subcategories/(.+?)([0-9]+)?$category/subcategories/$1[L,R=301]
,但我无法让它工作。

我想说这可能就是你想要的:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/subcategory/(.+?)-\d+\.html$ /category/subcategory/$1.html [R=301]
为此,您显然需要在http服务器中激活重写模块。如果你想使用动态配置文件(见下文…),你也需要启用它(见
AllowOverride
指令)



还有一个一般提示:您应该始终希望将此类规则放置在http服务器(虚拟)主机配置中,而不是使用动态配置文件(
.htaccess
样式文件)。这些文件是出了名的容易出错,难以调试,而且它们确实降低了服务器的速度。只有在您无法控制主机配置的情况下(读:非常便宜的主机服务提供商),或者如果您的应用程序依赖于编写自己的重写规则(这是一个明显的安全噩梦),才提供它们作为最后一个选项.

为什么不直接从备份中取出旧的
.htaccess
文件?@arkascha我希望我有一份备份副本。我有一个没有
.htaccess
文件。听到这个消息很难过。不管发生了什么。。。不过,我希望下面的回答能有所帮助;-)