Webdes
Webdes

Reputation: 11

How to redirect multiple old urls to new urls using a 301 redirect via htaccess

I have recently change some URLs from my website to make it more SEO friendly. I have old urls that I would like to link to new urls. The old urls are no longer available but I would like the old urls to redirect to the new urls.

My current 301 redirect code below is for http:// to https://www, which I need.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301,NC]

How do I make it so that I can also redirect from old urls to new urls as well. Is it possible with the 301 redirect?

Old url examples:
www.mywebsite.com/book-stories.html
www.mywebsite.com/book-journals.html

New url examples:
www.mywebsite.com/stories.html
www.mywebsite.com/journals.html

Any help would be much appreciated.

Upvotes: 1

Views: 61

Answers (2)

Webdes
Webdes

Reputation: 11

I tried the above code and didn't work and to be honest I could have been doing it incorrectly. I did look into it further and tried another 301 redirect

Redirect 301 book-stories.html https://www.mywebsite.com/stories.html
Redirect 301 book-journals.html https://www.mywebsite.com/journals.html

That worked for me. Hope this helps anyone else who will encounter this issue.

Upvotes: 0

msg
msg

Reputation: 8161

If you only have a handful, static redirections you can do it appending RewriteRules just as you are doing:

RewriteRule ^book-stories.html$ stories.html [R=301]
RewriteRule ^book-journals.html$ journals.html [R=301]

If they all have a fixed pattern, you could exploit that to reduce the amount of rules.

RewriteRule ^book-([a-z0-9.-]+).html$ $1.html [R=301]

If you have many different rules that you will be updating, you can look into indexed maps or even database-based RewriteMaps

Upvotes: 0

Related Questions