我要求重定向下面的许多网址:
www.example.com/test1/test2/P-b1_l1_id_c1_c1-10551-的 10051 -74560烯 至 www.example.com/test1/test2/P-b1_l1_id_c1_c1-10551-10551 -74560烯
只需更改10051到10551。
我本可以做简单的重定向,但有很多URL有相同的要求。因此,查看是否有任何解决方案可以替代所有页面的10551- 10051 - 至10551- 10551 在网址中有10551- 10051 - 。
提前致谢!!!
答案 0 :(得分:1)
这是在根目录的.htaccess文件中使用mod_rewrite指令的想法:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)-10551-10051-(.*)/?
RewriteRule .* %1-10551-10551-%2 [R=301,L]
# Enable rewriting engine
RewriteEngine On
# Specify the URL prefix
RewriteBase /
# Prevent loops
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match any URL the holds this string: -10551-10051-, splitting it in 3 parts
# The segment before the string to match, the string itself and the next segment up to the first slash or nothing
RewriteCond %{REQUEST_URI} ^/(.*)-10551-10051-([^/]+)/?
# Assemble the substitution URL like this: first segment-modified string-last segment. Rewrite.
RewriteRule .* %1-10551-10551-%2 [R=301,L]
这将重定向
http://example.com/Folder1/Folder2/anything1-10551-10051-anything2
要:
http://example.com/Folder1/Folder2/anything1-10551-10551-anything2
我假设你想要一个永久的,可见的重定向。如果不是这种情况,请删除R=301
[R=301,L]