Reputation: 23
I have many pages with similar urls like:
https://example.com/mainpage/1
https://example.com/mainpage/2
https://example.com/mainpage/3
.
..
https://example.com/mainpage/100
I want to redirect them using .htaccess
to new individual pages. Like:
https://example.com/mainpage/1
should redirect to https://example.com/mainpage-1
https://example.com/mainpage/2
should redirect to https://example.com/mainpage-2
https://example.com/mainpage/3
should redirect to https://example.com/mainpage-3
and so on.
since there are many pages of very similar URLs, I want to know if a single code of each kind can work.
P.S. : I have little knowledge of coding. Need help for my personal blog
Upvotes: 2
Views: 137
Reputation: 133428
Could you please try following, written and tested with shown samples. Please clear your browser cache before you test your URLs. 1st one is specific to shown samples, keeping mainpage string into 1st backreference and rest digits after /
in 2nd backreference which we are using them while redirecting on its right side part.
Specific to samples:
RewriteEngine ON
RewriteRule ^(mainpage)/([0-9]+)/?$ $1-$2 [R=302,NC,L]
Generic one:
RewriteEngine ON
RewriteRule ^([\w-]+)/([0-9]+)/?$ $1-$2 [R=302,L]
Upvotes: 4