Home > Back-end >  How to redirect if not root route to subdirectory index file with .htaccess
How to redirect if not root route to subdirectory index file with .htaccess

Time:05-26

I have two web pages. I want to deploy these two pages on one domain. When I call the root URL, I want to load the index.html in root directory, and for other URLs, I want to load the index1.html in the /app directory.

This is the directory structure.

www.example.com/index.html
www.example.com/app/index1.html

For example: when request www.example.com loading index.html

For www.example.com/login loading /app/index1.html

For www.example.com/signup loading /app/index1.html

This is what I have tried.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule ^(.*)$ /app/index1.html [R=301,L]
</IfModule>

This make redirection when I request www.example.com/signup to www.example.com/app/index1.html.

But I want to load app/index1.html without redirection. Please help me.

CodePudding user response:

With your shown samples, please try following .htaccess rules. Please make sure of following things before you test your URLs:

  • Make sure your .htaccess file, index.html and your app folder are residing in same root folder.
  • Make sure you have index1.html file present inside /app folder.
  • Make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##Rule from OP's attempt to block direct access of index.html file.
RewriteRule ^index\.html$ - [NC,L]

##Rule to handle only url www.example.com here.
RewriteRule ^/?$  /index.html [QSA,NC,L]

##Rules to handle rest of the cases here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^  /app/index1.html [QSA,NC,L]
  • Related