Home > front end >  How can I use .htaccess rewrite to redirect the root URL to a subdirectory, but also have a fallback
How can I use .htaccess rewrite to redirect the root URL to a subdirectory, but also have a fallback

Time:05-02

I'm trying to find a solution for changing root to default to subdirectory_1, but when where is no file/directory inside subdirectory_1, redirect all request to root directory.

Folders

.
├── /subdirectory_1
│   ├── index.html
│   ├── about.html
│   └── ...
├── index.php
└── ...

For me main goal is to have Static Generated Site in subdirectory_1, but also have CMS (like Wordpress) on root (as admin for some posts, later to use with API).

Example of what I am looking for:

If I call mysite.com/about it opens file from /subdirectory_1/about.html and shows in URL mysite.com/about (without subdirectory_1).

But if I call mysite.com/contacts and there is no contacts file (or directory) in /subdirectory_1 it should try to open /contacts in root directory

So far what have I found and tried is this in .htaccess file:

RewriteEngine On

# HTTP redirect to HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ mysite.com/$1 [R,L,NC]

# ROOT Transfer to subdirectory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/subdirectory_1 [NC]
RewriteRule ^(.*)$ /subdirectory_1/$1 [L]

# Redirects index (/) to subdirectory index first
RewriteRule ^(/)?$ subdirectory_1/index.html [L]

# Wordpress config
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

It works almost fine, but there are some weird behauvour sometimes, when I access some file it shows whole URL with subdirectory_1. For example, I call mysite.com/about and it opens mysite.com/subdirectory_1/about, and if I try right away again mysite.com/about it opens as it should mysite.com/about.

Somehow it's not consistant :/

Maybe there is a better, cleaner and more reliable solution? Or I just made a mistake somewhere in .htaccess?

CodePudding user response:

After discussion in one of the programmers' Discord, I finally find a working solution. So I decided to share it with everyone.

My new .htaccess file:

RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ mysite.com/$1 [R,L,NC]

# Exceptions
# For all Wordpress Admin related links
RewriteCond %{REQUEST_URI} ^/(wp-admin|wp-login)/ [NC]
RewriteRule ^ - [L]

# ensure that all directory requests include a trailing slash
RewriteCond %{REQUEST_URI} / [^\.] $
RewriteRule ^(. [^/])$ %{REQUEST_URI}/ [R,L]

# Make /subdirectory_1 like it was root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} /subdirectory_1/([^\s] ) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_URI} !^/subdirectory_1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /subdirectory_1/$1 [NC,L,QSA]

# Wordpress config
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Now static generated website (in subdirectory_1) works correctly and I still can access WordPress.

  • Related