Home > Software design >  Redirect all pages to new Subfolder under same Directory
Redirect all pages to new Subfolder under same Directory

Time:09-30

I am manually migrating a website to a different hosting service. Everything including the domain should remain the same. There is an old site in the root directory of this hosting service that needs to be preserved, and so I'm installing the new site in a subdirectory. I have created a subdirectoy for the website and have this in the .htaccess in the root directory:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.org$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.org$
RewriteRule ^(/)?$ subfolder/index.php [L] 
</IfModule>

This is redirecting the homepage correctly, but every other page is showing up with the old website view and giving a 404 error. I'm assuming there's a problem in my .htaccess, but I don't know how to locate it.

EDIT: There is an .htaccess file in the subdirectory, since it's a full wordpress install copied over from another environment. These are the contents of the file:

Options  FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirs Base Redirect:
Redirect 301 /redirs https://redirs.io/containers/?co=client-organization&con=example.org&host=WP-Engine&gsheet=https://docs.google.com/spreadsheets/d/19Hw1ApeEuzlW5P0e2A7aZ6lJBWCN9Kpkvtvg8LFokik/edit#gid=0&gfile301=https://docs.google.com/spreadsheets/d/e/2PACX-1vRs5snE204cb7OZB-xFep5Ju9rccjKaNX-jP9Nb0yZHGlXQ-4pPh63-K8EP7YErCQwbFgYJSf52zlBP/pub?gid=0&single=true&output=csv&gfile302=https://docs.google.com/spreadsheets/d/e/2PACX-1vRs5snE204cb7OZB-xFep5Ju9rccjKaNX-jP9Nb0yZHGlXQ-4pPh63-K8EP7YErCQwbFgYJSf52zlBP/pub?gid=1333061259&single=true&output=csv&gfileDomRules=https://docs.google.com/spreadsheets/d/e/2PACX-1vRs5snE204cb7OZB-xFep5Ju9rccjKaNX-jP9Nb0yZHGlXQ-4pPh63-K8EP7YErCQwbFgYJSf52zlBP/pub?gid=1293752576&single=true&output=csv&gfileDoms=https://docs.google.com/spreadsheets/d/e/2PACX-1vRs5snE204cb7OZB-xFep5Ju9rccjKaNX-jP9Nb0yZHGlXQ-4pPh63-K8EP7YErCQwbFgYJSf52zlBP/pub?gid=547864294&single=true&output=csv

# Redirects with sources which do NOT contain a query string:
RedirectMatch 301 ^/parents-of-rsym/?$ https://example.org/youth/parents-of-river/
RedirectMatch 301 ^/index.php/mens-page/?$ https://example.org/mens-ministry/
RedirectMatch 301 ^/index.php/about/what-to-expect/?$ https://example.org/what-to-expect/
RedirectMatch 301 ^/index.php/about/staff-pr/?$ https://example.org/staff/
RedirectMatch 301 ^/index.php/about/statement-of-faith/?$ https://example.org/we-believe/
RedirectMatch 301 ^/index.php/about/contact-link/?$ https://example.org/contact/
RedirectMatch 301 ^/index.php/youth-page/?$ https://example.org/youth-group-ministry/
RedirectMatch 301 ^/index.php/womens-page/womens-ministry-blog/?$ https://example.org/womens-ministry-blog/
RedirectMatch 301 ^/index.php/resources/calendar2/?$ https://example.org/calendar/
RedirectMatch 301 ^/index.php/womens-page/?$ https://example.org/womens-ministry/
RedirectMatch 301 ^/index.php/96-recipes/?$ https://example.org/womens-ministry/
RedirectMatch 301 ^/?$ https://example.org/youth-group-ministry/
RedirectMatch 301 ^/index.php/programs/youth-page-2/youth-program-info-page/?$ https://example.org/youth-group-ministry/
RedirectMatch 301 ^/index.php/video/?$ /https://arlington.online.church/?fbclid=IwAR0IjoHt20yVnYZv-lbIKlgq1o-kLr1EbbfShSba3A6hAgMuzPMbZBlCCqs
RedirectMatch 301 ^/index.php/special-services/apologetics-forum-of-snohomish-county-afsc/?$ https://www.apologeticsforum.org
RedirectMatch 301 ^/index.php/programs/children-s-programs/vbs-form-signup-2/?$ https://example.org/childrens-ministry/
RedirectMatch 301 ^/index.php/programs/children-s-programs/how-can-children-sit-through-church/?$ https://example.org/how-can-children-sit-through-church/
RedirectMatch 301 ^/index.php/programs/children-s-programs/?$ https://example.org/childrens-ministry/
RedirectMatch 301 ^/index.php/programs/music-ministry-2/philosophy/?$ https://example.org/worship/
RedirectMatch 301 ^/index.php/programs/music-ministry-2/song-suggestions/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/programs/music-ministry-2/info-page/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/programs/music-ministry-2/girls-choir-info-link/?$ https://example.org/music-ministry/4-this-time-girls-choir/
RedirectMatch 301 ^/index.php/programs/music-ministry-2/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/programs/youth-page-2/?$ https://example.org/youth-group-ministry/
RedirectMatch 301 ^/index.php/programs/college-info-page-2/?$ https://example.org/young-adults/
RedirectMatch 301 ^/index.php/programs/womens-page-2/womens-ministry-pics/?$ https://example.org/womens-ministry/
RedirectMatch 301 ^/index.php/programs/womens-page-2/womens-ministry-blog/?$ https://example.org/womens-ministry/
RedirectMatch 301 ^/index.php/programs/womens-page-2/?$ https://example.org/womens-ministry/
RedirectMatch 301 ^/index.php/programs/mens-page-2/podcasts/iron-sharpens-iron/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/programs/mens-page-2/podcasts/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/programs/mens-page-2/men-min-pics-link/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/programs/mens-page-2/current-mens-link/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/programs/mens-page-2/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/programs/seniors-page-2/?$ https://example.org/senior-ministry/
RedirectMatch 301 ^/index.php/programs/?$ https://example.org/#ministries-church
RedirectMatch 301 ^/index.php/resources/altar-flower-gifts/?$ https://example.org
RedirectMatch 301 ^/index.php/resources/news-page/past-event-link/?$ https://example.org/calendar/
RedirectMatch 301 ^/index.php/resources/news-page/?$ https://example.org/calendar/
RedirectMatch 301 ^/index.php/resources/web-links/?$ https://example.org/calendar/
RedirectMatch 301 ^/index.php/resources/visitor-survey/?$ https://example.org
RedirectMatch 301 ^/index.php/resources/song-suggestions/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/resources/sermons-page/2016/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/sermons-page/2015/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/sermons-page/2014/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/sermons-page/2013/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/sermons-page/2012/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/sermons-page/2011/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/sermons-page/2010/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/sermons-page/2009/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/sermons-page/2008/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/sermons-page/?$ https://example.org/media-library/
RedirectMatch 301 ^/index.php/resources/prayer-requests/?$ https://example.org/how-can-we-help/?ineedparam=Prayer
RedirectMatch 301 ^/index.php/resources/subscribe-to-our-mailing-list/?$ https://example.org
RedirectMatch 301 ^/index.php/resources/calendar2/calendar2-2/?$ https://example.org/calendar/
RedirectMatch 301 ^/index.php/resources/calendar2/calendar2/?$ https://example.org/calendar/
RedirectMatch 301 ^/index.php/resources/?$ https://example.org
RedirectMatch 301 ^/index.php/womens-page/womens-ministry-pics/?$ https://example.org/womens-ministry/
RedirectMatch 301 ^/index.php/womens-page/?$ https://example.org/womens-ministry/
RedirectMatch 301 ^/index.php/college-info-page/?$ https://example.org/young-adults/
RedirectMatch 301 ^/index.php/children-s-programs/how-can-children-sit-through-church/?$ https://example.org/bringing-children-to-church/
RedirectMatch 301 ^/index.php/children-s-programs/nursery/?$ https://example.org/childrens-ministry/nursery/
RedirectMatch 301 ^/index.php/children-s-programs/bible-day-camp/?$ https://example.org/childrens-ministry/
RedirectMatch 301 ^/index.php/children-s-programs/kids-club/?$ https://example.org/childrens-ministry/kids-club/
RedirectMatch 301 ^/index.php/children-s-programs/?$ https://example.org/childrens-ministry/
RedirectMatch 301 ^/index.php/about/staff-pr/staff/?$ https://example.org/staff/
RedirectMatch 301 ^/index.php/about/directions-page/?$ https://example.org/about-us/
RedirectMatch 301 ^/index.php/about/service-times/?$ https://example.org/about-us/
RedirectMatch 301 ^/index.php/about/?$ https://example.org/about-us/
RedirectMatch 301 ^/index.php/mens-page/podcasts/iron-sharpens-iron/?$ https://example.org/mens-ministry/
RedirectMatch 301 ^/index.php/mens-page/podcasts/?$ https://example.org/mens-ministry/
RedirectMatch 301 ^/index.php/mens-page/men-min-pics-link/?$ https://example.org/mens-ministry/
RedirectMatch 301 ^/index.php/seniors-page/?$ https://example.org/senior-ministry/
RedirectMatch 301 ^/index.php/music-ministry/philosophy/?$ https://example.org/worship/
RedirectMatch 301 ^/index.php/music-ministry/song-suggestions/?$ https://example.org/worship/
RedirectMatch 301 ^/index.php/music-ministry/girls-choir-info-link/?$ https://example.org/music-ministry/4-this-time-girls-choir/
RedirectMatch 301 ^/index.php/music-ministry/music-ministry/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/music-ministry/?$ https://example.org/music-ministry/
RedirectMatch 301 ^/index.php/missions/past-mission-teams/?$ https://example.org/missionaries/
RedirectMatch 301 ^/index.php/missions/missions/missionaries/?$ https://example.org/missionaries/
RedirectMatch 301 ^/index.php/missions/missions/?$ https://example.org/missionaries/
RedirectMatch 301 ^/index.php/missions/?$ https://example.org/missionaries/
RedirectMatch 301 ^/index.php/youth-page/youth-program-info-page/?$ https://example.org/youth-group-ministry/
RedirectMatch 301 ^/index.php/?$ https://example.org
# Redirects with sources which contain a QUERY STRING:

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


CodePudding user response:

RewriteRule ^(.*)$ /subfolder/$1

Unless you have an additional .htaccess file in the subfolder then this RewriteRule should also rewrite the request directly to subfolder/index.php (the same as the last rule). For example:

RewriteRule . subfolder/index.php [L]

Since the backreference is not required, I have also reduced the RewriteRule pattern to a single dot (so it matches everything except the root - which is handled by the next rule).

You were also missing the L flag. No more rules should be processed when this is successful.


UPDATE:

EDIT: There is an .htaccess file in the subdirectory

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

You can revert the root .htaccess file to how you had it originally, except that you should include the L flag on that rule and remove the slash prefix. For example:

:
RewriteRule (.*) subfolder/$1 [L]

The start and end anchors (ie. ^ and $) are not required on the RewriteRule pattern.

HOWEVER, you need to change the rules in the /subfolder/.htaccess file:

  1. Remove the RewriteBase directive entirely. Note that this currently appears twice in the file (at the top and in the WP code block at the end). Both instances must be removed.

  2. Remove the slash prefix on the substitution string in the last rule in the WordPress code block. In other words, change this:

    RewriteRule . /index.php [L]
    

    To this:

    RewriteRule . index.php [L]
    

However, a potential problem with this is as the preceding comment warns... WordPress attempts to maintain this section, so you need to prevent WordPress from overriding these changes!

  • Related