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 yourapp
folder are residing in sameroot
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]
CodePudding user response:
<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>
You basically just needed to remove the R
(redirect
) flag on the last RewriteRule
directive. But this can be optimised:
DirectoryIndex index.html
RewriteEngine On
RewriteRule ^app/index1\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . app/index1.html [L]
mod_dir (ie. DirectoryIndex
) serves /index.html
from the root. This may already be configured in the server config, so the DirectoryIndex
directive may not be required here.
The first
RewriteRule
directive is an optimisation to prevent unnecessary filesystem checks. This should match the file being rewritten to. ie./app/index1.html
(not/index.html
).The last
RewriteRule
matches against a single character (ie..
- dot) so excludes requests for the root directory so prevents unnecessary filesystem checks everytime the root is requested. The regex^(.*)$
on the other hand matches everything, including the root (which fails the directory check - 2nd condition /RewriteCond
directive).Unless you are using symlinks then you can remove the 3rd condition.
Depending on the format of your URLs then you could make the regex more restrictive and perhaps remove the first condition that checks that the request does not map to a file (filesystem checks are relatively expensive). eg. Do your URLs contain dots? The two examples you gave do not. Dots naturally delimit file extensions, so if your URLs do not contain dots then they will naturally not map to any existing files.