Load website in a subdirectory using .htaccess

Viewed 51

I need to make a fully working backup of the actual site in a subdirectory, but I don't understand why it doesn't work, I mean the website is not showing correctly.

Here is the .htaccess code of the root directory. I need to make a backup in a subdirectory "/old/".


RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%example.com%{REQUEST_URI} [R=301,L]

RewriteRule ^([^/]*)/css/(.*) /css/$2 [L]
RewriteRule ^([^/]*)/js/(.*) /js/$2 [L]
RewriteRule ^([^/]*)/fonts/(.*) /fonts/$2 [L]
RewriteRule ^([^/]*)/images/(.*) /images/$2 [L]
RewriteRule ^([^/]*)/templates/(.*) /templates/$2 [L]


RewriteRule ^([^/]*)/$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?lang=$1 [L]

Thank you for your help!

1 Answers
RewriteRule ^([^/]*)/css/(.*) /css/$2 [L]
RewriteRule ^([^/]*)/js/(.*) /js/$2 [L]
RewriteRule ^([^/]*)/fonts/(.*) /fonts/$2 [L]
RewriteRule ^([^/]*)/images/(.*) /images/$2 [L]
RewriteRule ^([^/]*)/templates/(.*) /templates/$2 [L]

RewriteRule ^([^/]*)/$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?lang=$1 [L]

You just need to remove the slash prefix from all the substitution strings in the above directives. You can then use the same .htaccess file in both the root and in the backup subdirectory.

For example:

RewriteRule ^([^/]*)/css/(.*) css/$2 [L]
RewriteRule ^([^/]*)/js/(.*) js/$2 [L]
RewriteRule ^([^/]*)/fonts/(.*) fonts/$2 [L]
RewriteRule ^([^/]*)/images/(.*) images/$2 [L]
RewriteRule ^([^/]*)/templates/(.*) templates/$2 [L]

RewriteRule ^([^/]*)/$ index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ index.php?lang=$1 [L]

Removing the slash prefix makes the substitution relative to the directory that contains the .htaccess file.

These 9 directives can be reduced/simplified into 2 if you wish:

RewriteRule ^[^/]+/((?:css|js|fonts|images|templates)/.*) $1 [L]
RewriteRule ^([^/]+)(?:/[^/]*){1,3}$ index.php?lang=$1 [L]

UPDATE: I've fixed the regex in the 2nd "simplified" rule since when there is only one path segment the trailing slash is mandatory (previously it would not have matched if the trailing slash was included). For two and three path segments the trailing slash is effectively optional and for four path segments, there should be no trailing slash. Although whether that was really the intention of the original directives is another matter. The 2nd "simplified" rule now matches that behaviour. (Previously, it did not permit a trailing slash with any number of path segments.)


Aside:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%example.com%{REQUEST_URI} [R=301,L]

You should reverse the order of these two rules. Otherwise, the first rule will prevent the canonical redirect (that prefixes the www subdomain) from happening on direct file requests.

Related