Home > Software design >  When the website URL is typed in, the .htaccess must instantly go to the correct file & path
When the website URL is typed in, the .htaccess must instantly go to the correct file & path

Time:09-30

When the website URL is typed in correctly like: https://example.amsterdam

It should instantly change the URL to: https://example.amsterdam/dist/index.php

I already tried to solve this problem with creating a DirectoryIndex dist/index.php The only clue with trying to fix it like this is it only loads up the page and not the correct path in the URL. So when the file is open and you click on a button,

the HREF doesn't work anymore because the path is not correct.

So I basically want to open the file in the correct path in the URL. Instantly when the user types the URL

My current .htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

Current Filestack

CodePudding user response:

This does sound like an X-Y problem, as "redirecting" to the DirectoryIndex document is generally not desirable and sounds like you are compensating for other issues. eg. Using relative URL-paths in your links (and to static assets), exposing the /dist/ subdirectory when this should be hidden, etc.

Anyway, to answer your question specific question...

I already tried to solve this problem with creating a DirectoryIndex dist/index.php

If you specifically want to redirect the request (rather than an internal subrequest) then you can set DirectoryIndexRedirect on in addition to the DirectoryIndex directive. In other words:

DirectoryIndex dist/index.php
DirectoryIndexRedirect on

The above would trigger a 302 (temporary) redirect. You can use the http status code 301 for a permanent redirect. See the docs for other options.

Note that the DirectoryIndex directive applies to any directory that you request. So, if you were to request /dist/ then the above directive would cause mod_dir to look for /dist/dist/index.php (since you had specified a relative path). You could use DirectoryIndex /dist/index.php (a root-relative path) to always serve the same file, regardless of whether a subdirectory is requested.

Alternatively, to only redirect the document root then use mod_rewrite. For example:

RewriteRule ^$ /dist/index.php [R,L]

Again, the above is a 302 (temporary) redirect. Use R=301 for a permanent redirect.

Although you shouldn't need index.php to be present in the (visible) URL, so just redirect the root to /dist/ and let a standard DirectoryIndex do the work. For example:

DirectoryIndex index.php

RewriteRule ^$ /dist/ [R,L]

Reference:

  • Related