How To Redirect an Entire Folder

How To Redirect an Entire Folder or Directory

Redirecting an Entire Folder or Directory

If you’d like to redirect a whole directory (commonly called a ‘folder’), the code is not that much different from a specific page redirect.

How To Redirect an Entire Folder or Directory

Redirecting/redirect an entire whole folder:

Assuming that the user is looking for the page “http://www.example.com/folder/samplepage.html“, – these different directives would redirect to the respective URLs as shown below:

Use For: Renaming a folder or directory & keep old files where they are

How To Redirect an Entire Folder or Directory

redirect 301 /folder/ http://www.example.com/newfolder/

So,

http://www.example.com/folder/samplepage.html

would resolve to

http://www.example.com/newfolder/samplepage.html

All old pages within the “/folder/” directory now are copied into the new destination URL “/newfolder/”.

For multiple directories, if your folder is deeper than the first level, list the preceding folders in your script, as such:

redirect 301 /level1/folder/ http://www.example.com/level1/newfolder/

thusly,

http://www.example.com/level1/folder/samplepage.html

would resolve to

http://www.example.com/level1/newfolder/samplepage.html

Use For: Renaming a folder or directory & pointing ALL PAGES to a common new location.

How To Redirect an Entire Folder or Directory

redirectMatch 301 ^/folder/ http://www.example.com/newfolder/

http://www.example.com/folder/samplepage.html

would resolve to

http://www.example.com/newfolder/

All old pages within the “/folder/” directory are now lost and are pointed to the new destination URL “/newfolder/”.

To point all pages in a specific folder to a specific page, add the new page to the url in your script, as such:

redirectMatch 301 ^/folder/ http://www.example.com/newfolder/newpage.html

thusly,

http://www.example.com/folder/samplepage.html

would resolve to

http://www.example.com/newfolder/newpage.html

Use For: Moving and keeping files or pages in a new and/or deeper level directory (adding a subdirectory)

How To Redirect an Entire Folder or Directory

redirectMatch 301 ^/folder/(.+)$ http://www.example.com/newfolder/xyz/$1

So,

http://www.example.com/folder/samplepage.html

would resolve to

http://www.example.com/newfolder/xyz/samplepage.html

Here, “newfolder” is substituted in place of “/folder/”, then “/xyz” is inserted, and finally the old page “samplepage.html” is copied into the new destination URL).

INCORRECT USE:

redirectMatch 301 ^/folder/$ http://www.example.com/newfolder/

A redirect will not occur in this instance. The requested URL MUST end in “/folder/” exactly.

This example only illustrates some of the possibilities, but it should be enough to get you going.

Here’s a link to safely redirecting an entire website to a new URL location.

Leave a Reply

Your email address will not be published. Required fields are marked *