1

How can I return an index.json file in a folder when the folder is navigated to using .htaccess?

I have the following directory and file structure:

/api/.htaccess
/api/index.json
/api/page-1/index.json
/api/page-1/page-a/index.json
/api/page-1/page-b/index.json
/api/page-2/index.json
/api/page-2/page-a/index.json
/api/page-2/page-b/index.json
/api/page-foo/index.json

and I would like them to appear when you navigate to the following URLs:

/api/
/api/page-1/
/api/page-1/page-a/
/api/page-1/page-b/
/api/page-2/
/api/page-2/page-a/
/api/page-2/page-b/
/api/page-foo/

How can I update the api/.htaccess file to allow this? So far all I have working is this bit:

RewriteEngine On
RewriteRule ^/?$ index.json [L]
AddType application/json json

Which is successfully showing /api/index.json at the /api/ endpoint, but it doesn't point /api/page-1/ to /api/page-1/index.json

1 Answer 1

1

You don't need a rule for this. Just use DirectoryIndex:

DirectoryIndex index.json
Sign up to request clarification or add additional context in comments.

1 Comment

You can remove RewriteRule ^/?$ index.json [L] rule as well after this directive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.