【发布时间】:2015-06-22 14:45:14
【问题描述】:
我列出了一堆产品
https://www.example.com/all/products/foldername/1234567890-ProductDescriptionA-456.html
https://www.example.com/all/products/foldername/7654321-SomeOtherDesc-B123.html
https://www.example.com/all/products/foldername/93939393-anotherthing-F93939393.html
我希望这些被重定向到
https://www.example.com/products.php?p=1234567890
https://www.example.com/products.php?p=7654321
https://www.example.com/products.php?p=93939393
分别。是否有 htaccess 规则对匹配的参数进行字符串操作?例如,如果我必须使用 Python 转换我的 URL,它看起来像这样:
def make_new_url(old_url):
product_id = old_url.split('/')[-1].split('-')[0]
new_url = 'https://www.example.com/products.php?p=%s' % product_id
return new_url
在旧网址中,产品 ID 位于最后一个“/”之后和第一个“-”之前。另一个可行的基于正则表达式的规则是:
\/(\d*?)\-
关于如何在 Apache htaccess 文件中完成此操作有什么想法吗?
【问题讨论】:
-
您希望在浏览器中将 URL 更改为
https://www.example.com/products.php?p=1234567890吗? -
是的。当向服务器发出
https://www.example.com/all/products/foldername/1234567890-ProductDescriptionA-456.html的请求时,我希望返回https://www.example.com/products.php?p=1234567890。我不关心除了 SOMENUMBERS 中的任何内容:...../foldername/SOMENUMBERS-otherstuff.html -
嗯,那么 2 个答案中的任何一个都应该适合您。