0

I recently switched to Wordpress and I dont want to lose some of the old pages. The old website had pages using the following URL format

domain.name/gallery/word1_word2_word3_wordX.html

I need these links to be converted to:

domain.name/?s=word1+word2+word3+wordX&post_type=product

Basically I need to get everything after gallery/, remove .html, replace the underscores with plus sign and pass this to the new URL format between domain.name/?s= and &post_type=product

Any help is appreciated. Thank you.

proone
  • 3
  • 2
  • Check out http://stackoverflow.com/a/1279758/2057919 – elixenide Nov 10 '15 at 18:41
  • Actually that was the first method I tried to modify for my situation, but my mod_rewrite abilities are very limited and the result wasnt successful. – proone Nov 11 '15 at 11:11

1 Answers1

1

I played with this a bit and came up with a solution that works:

RewriteEngine On

# Note: This line is important! Without it, your URLs will look like example.com/home/you/public_html/gallery/etc, which is bad.
RewriteBase /

# Replace any _ with + and redirect
RewriteRule ^gallery\/(.*)_(.*) gallery/$1+$2 [R=301]
# Replace gallery/whatever.html with /?s=whatever&post_type=product
RewriteRule ^gallery/(.*)\.html /?s=$1&post_type=product [R=301]

This turns

http://example.com/gallery/foo_bar_baz_bam.html

into

http://example.com/?s=foo+bar+baz+bam&post_type=product

I drew on this post and this one to come up with this; you may find those helpful, as well.

Note: This does a redirect for each stage of the rewrite (one per _ and one for the final transformation). In theory, it's possible to do only one redirect by using [N] instead of [R=301] in the first RewriteRule (the .htaccess equivalent of a while loop), but I couldn't get this working without creating an infinite loop.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100