Problem:
Some of our redirects in our main web application were acting a little screwey. Turns out our stand-alone URL redirect app didn't play well with URLs with a trailing "/". I needed to simply trim all trailing "/" from URLs that have them.
Solution:
Here's the script I created:
The meat and potatoes is the highlighted update + case statement. I also added some selects to see the results of the execution. I naturally wrap my statements in a transaction and ROLLBACK in case bad things happen. Once you're confident executing, change ROLLBACK to COMMIT.
Results:
This is a simple output of the results just to see if it affected anything.
Hope this helps somebody.
Some of our redirects in our main web application were acting a little screwey. Turns out our stand-alone URL redirect app didn't play well with URLs with a trailing "/". I needed to simply trim all trailing "/" from URLs that have them.
Solution:
Here's the script I created:
BEGIN TRANSACTION
SELECT [url]
AS 'url with
trailing "/"'
FROM [URLRedirects].[dbo].[Redirects]
WHERE RIGHT([url], 1) = '/'
UPDATE [Redirects]
SET [url]
= CASE RIGHT([url], 1)
WHEN '/' THEN LEFT([url], Len([url]) - 1)
ELSE [url]
END
WHERE RIGHT([url], 1) = '/'
SELECT 'Updated '
+ CONVERT(VARCHAR, @@ROWCOUNT) + ' records'
SELECT CONVERT(VARCHAR, Count([url]))
+ '
records with trailing "/" after update'
FROM [URLRedirects].[dbo].[Redirects]
WHERE RIGHT([url], 1) = '/'
SELECT [url]
FROM [URLRedirects].[dbo].[Redirects]
ROLLBACK TRANSACTION
The meat and potatoes is the highlighted update + case statement. I also added some selects to see the results of the execution. I naturally wrap my statements in a transaction and ROLLBACK in case bad things happen. Once you're confident executing, change ROLLBACK to COMMIT.
Results:
This is a simple output of the results just to see if it affected anything.
Hope this helps somebody.
Comments
Post a Comment