1

I have a link https://example.com/src/index.php, and want index.php, and other pages, which are in src folder, to be accessible without src in the URL.

My .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
2
  • Is your existing .htaccess file an attempt at a solution? Those directives (although incorrect) would imply you are implementing extensionless URLs, although this is not stated in your question? Commented Jun 4, 2022 at 17:44
  • Those directives are for another purposes, I know. It's not my solution, I just wanted to show an existing .htaccess file Commented Jun 4, 2022 at 17:55

1 Answer 1

1

You can do this using mod_rewrite in the root .htaccess file. For example:

# Serve file from "/src" subdirectory if it exists
RewriteCond %{DOCUMENT_ROOT}/src/$0 -f
RewriteRule ^.+\.\w{2,4}$ src/$0 [L]

If you request /index.php and this file exists at /src/index.php then it will internally rewrite the request to that subdirectory.

The regex ^.+\.\w{2,4}$ matches only files (ie. URLs that include a file extension).

$0 is a backreference to the URL-path that is matched by the RewriteRule pattern.

If a file happened to exist in both places then the file in the /src subdirectory would take priority.

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

Comments

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.