I have a website on my subdirectory.
My all URL was mywebsite.com/sub/page-or-post-link
I have moved my site to the main directory. mywebsite.com.
I want to redirect my old link to new links.
Like if someone searches mywebsite.com/sub/a-post-or-page.
They should redirect to mywebsite.com/a-post-or-page
How can I do this with javascript, jquery, or with changing .hraccess?
You can use RewriteRule in .htaccess file. This one should make a job:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^sub/(.*)$ http://mywebsite.com/$1 [R=301,L]