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]
CodePudding user response:
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.