Using .htaccess to set a sub directory as root directory [closed] Using .htaccess to set a sub directory as root directory [closed] php php

Using .htaccess to set a sub directory as root directory [closed]


If you want to keep the 'href="/css/layout.css"' part, then yes, you can set up a rewrite rule. You just have to be careful not to redirect anything starting with /testsite (or you'll end up with a loop).

Like (in root .htaccess):

RewriteEngine OnRewriteCond %{REQUEST_URI} !^/testsite/.*$RewriteRule ^(.*)$ /testsite/$1 [QSA,L]

There, you will be able to access your layout.css file either from:

http://localhost/testsite/css/layout.css

or

http://localhost/css/layout.css


Sure, all relative URIs (as yours) are relative to the base URI which by default is the URI of the document:

http://localhost/testsite/index.php

You only need to tell which part to add:

<link rel="stylesheet" type="text/css" href="./css/layout.css" />                                             ^                                             ` see this dot

Example:

Base URI      ::  http://localhost/testsite/index.phpRelative URI  ::  ./css/layout.cssResult        ::  http://localhost/testsite/css/layout.css

If you need to have this more modular, there are multiple ways to do that. One is to set the base URI explicitly inside the HTML document (see <base>).

Another one is to resolve links relatively on the server-side based on the Request URI and your site's root-path. See this answer for an example.