0

I have requirement to redirect many URLs are below:

www.example.com/test1/test2/P-b1_l1_id_c1_c1-10551-10051-74560-en to www.example.com/test1/test2/P-b1_l1_id_c1_c1-10551-10551-74560-en

just a change 10051 to 10551.

I could have done simple redirect but there are many URL with same requirement. So looking if there is any solution which can replace 10551-10051- to 10551-10551- for all pages which have 10551-10051- in the URL.

Thanks in advance!!!

4

1 回答 1

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]

于 2013-01-21T11:13:13.380 回答