Please could I have some help redirecting URLs to a subfolder so that everything in "https://site1.co.uk" is rewritten to "https://site1.co.uk/subfolder"? The .htaccess file needs to be in /subfolder. Many thanks in advance.
CodePudding user response:
Do this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. )$ /subfolder/$1 [QSA,L]
</IfModule>
CodePudding user response:
You could do something like the following in the root .htaccess
file to rewrite all direct requests to the subfolder.
RewriteEngine On
# Rewrite all direct requests to the `/subfolder` directory
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^ subfolder%{REQUEST_URI} [L]
The check against the REDIRECT_STATUS
environment variable ensures that only direct requests from the client (as opposed to internally rewritten requests) are rewritten to the /subfolder
directory (thus avoiding a rewrite loop).
This also means that a direct request for /subfolder
itself will also be rewritten to the /subfolder
directory (ie. /subfolder/subfolder
) - so you can have a sub-subfolder of the same name if you wish (otherwise direct requests for /subfolder
will result in a 404). That is, unless you have an additional .htaccess
file in the subfolder that also contains mod_rewrite directives, in which case these directives will be overridden.
UPDATE:
The
.htaccess
file needs to be in/subfolder
.
In that case, this is not possible. The .htaccess
file that rewrites requests to the /subfolder
must be in the document root (the parent directory).
If the .htaccess
file is in the /subfolder
itself then it will simply never be processed for requests outside of that subfolder in order to rewrite the request.