Redirect support

#1

I have a site setup for a friend where I was using TCMS blog to show a list of inventory for two sets of products. So their site was:
example.com/inventory/productA and
example.com/inventory/productB

They’ve now downsized to just sell the one so I’ve removed productB and updated the navigation to:
example.com/productA

I think I can work out how to redirect the main url but I’m not entirely sure how to make sure that existing posts get redirected. (Example, if a post is:
example.com/inventory/productA/details/?permalink=url-string
how to make sure it goes to
example.com/productA/details/?permalink=url-string

Is there a single rule I can put in or would I need to manually redirect all existing posts?

#2

They only had about 20 active listings so I went and just manually redirected them. Would still be curious for the future if there’s a simple/single htaccess rule that would accomplish the above.

#3

It should be easy enough to do with a simple RewriteRule set.
Since you are giving very generic URL examples I can’t say for sure if this would work but something like this would be a starting point:

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/inventory/productA/details
RewriteCond %{QUERY_STRING} ^permalink=url
RewriteRule ^(.*)$ https://www.example.com/productA/details/%1 [R=301,L]
1 Like
#4

Thanks!
I don’t know why I’m being cagey with the details.

The site is https://gilligansboats.ca/ and it once had a spot for /inventory/boats and /inventory/trailers. Now it’s just boats.
A sample blog post would be:
https://gilligansboats.ca/inventory/boats/details/?permalink=2019-mb-sports-f22-tomcat-1554926454 and the new one would be:
https://gilligansboats.ca/boats/details/?permalink=2019-mb-sports-f22-tomcat-1554926454

#5

OK so if I understand, The entire trailers section is gone? you are merging the post from /inventory/trailers/details and /inventory/boats/details into /boats/details/?

If that’s true than this should work:

RewriteCond %{REQUEST_URI}  ^(\/inventory/boats|\/inventory/trailers)
RewriteCond %{QUERY_STRING} ^permalink=
RewriteRule ^(.*)$ https://gilligansboats.ca/boats/details/%1 [R=301,L]

right now the “made with Love htaccess tester” is down so I haven’t been able to test it.

To explain the first rewrite condition
RewriteCond %{REQUEST_URI} ^(\/inventory/boats|\/inventory/trailers)
Simple checks the server variable REQUEST_URI ( after domain name) starts with \/inventory/boats or \/inventory/trailers, if that’s true it goes to the second rewrite condition:
RewriteCond %{QUERY_STRING} ^permalink=
This one checks to see if there is a query string (stuff after ? in the URL) and that it starts with permalink=. If that is true as well then the RewriteRule is applied:
RewriteRule ^(.*)$ https://gilligansboats.ca/boats/details/%1 [R=301,L]
The %1 should apply the query string as entered.

Right now the only decent tester I know about(link above) is getting a 525 error.

2 Likes
#6

That name looks familiar !

I was just about to post an excellent link to the RW forum where one helpful Teefers walked me through a similar question :-)

#7

He’s a legend!

#8

Only in my own mind…
The tester https://htaccess.madewithlove.be/ is back up and working.
I made a couple quick changes and tested with the one sample above:

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^(/inventory/boats|/inventory/trailers)
RewriteCond %{QUERY_STRING} ^permalink=
RewriteRule ^(.*)$ https://gilligansboats.ca/boats/details/ %1 [R=301,L]

I remove an unneeded escape \ char and put a space between the last / and the variable %1. It now should work fine.

2 Likes