user137
user137

Reputation: 759

.htaccess rewrite content to subfolder

I have the following folders structure on my web server:

Folder structure

I need some rewrite rules:

  1. When I visit http://testdomain1.com/ I need to set /pages/testdomain1.com/ as the root directory to display content from this dir.

    Similarly, http://testdomain2.com/ root folder is /pages/testdomain2.com/ etc..

  2. When the requested URL has the la var - I need to rewrite to landing directory.

    Example: http://testdomain1.com/?la=template1 - set /lands/template1/ as root dir

I have some issue with my current .htaccess (for first task):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?testdomain1.com$
RewriteRule ^(/)?$ pages/%{HTTP_HOST}/index.html [L]

Rewrite rule works, but all assets data (js, css) don't work (404 not found). How can I set the right rules?

Upvotes: 1

Views: 266

Answers (1)

MrWhite
MrWhite

Reputation: 45829

... but all assets data (js,css) don't work (404 not found). How can I set right rules?

This is most probably caused by using relative URL paths to your assets. You need to "fix" your URL paths to be root-relative or absolute. This is not something that should be fixed in .htaccess, your rewrite is otherwise OK.

See my answer to the following question on the Webmasters stack, relating to the use of relative URL paths to static assets when using URL rewriting:

RewriteCond %{HTTP_HOST} ^(www.)?testdomain1.com$
RewriteRule ^(/)?$ pages/%{HTTP_HOST}/index.html [L]

This only rewrites requests for the root, ie. testdomain1.com/. And not testdomain1.com/foo - is that intentional?

I need to have access to any page (example.com/page.html) not only index.html.

If by that you mean that a request of the form http://testdomain1.com/foo should be rewritten to /pages/testdomain1.com/foo, then you would need to change the above to something like the following:

RewriteCond %{QUERY_STRING} !^la=[\w-]+
RewriteCond %{HTTP_HOST} ^(?:www.)?(testdomain1\.com)$
RewriteRule !^pages/ pages/%1%{REQUEST_URI} [L]

Note that this rewrites everything, including CSS, JS and images.

The first condition (RewriteCond directive) excludes URLs that contain the la URL parameter. (These URLs are handled below.)

This also fixes a bug in your existing directive, whereby a request for the www subdomain (eg. www.testdomain1.com) would have resulted in an incorrect rewrite. The %1 backreference in the substitution string contains the domain being requested - less an optional www subdomain. (I assume you don't have a subdirectory www.testdomain1.com?) Although, arguably, you should already be canonicalising the www/non-www earlier in the config file.

(Remember to escape literal dots.)

UPDATE:

i tried to use this case for multiple domains, not only for testdomain1.com. So i need change RewriteCond %{HTTP_HOST} ^(www.)?testdomain1.com$ part. I just tried RewriteCond %{REQUEST_URI} but don't working.

For multiple domains, you can either:

  • Duplicate the above code block for each domain and change the testdomain1.com domain in the second condition.

  • If this applies to just 3 domains then you can use alternation in the second condition, to match any of the 3 domains. For example:

    RewriteCond %{HTTP_HOST} ^(?:www.)?(testdomain1\.com|testdomain2\.com|testdomain3\.com)$
    
  • If this should apply to any domain, then make the regex more general to match any domain. For example:

    RewriteCond %{HTTP_HOST} ^(?:www.)?([a-z0-9.-]+?)\.?$
    

2) When request URL have "la" var - I need rewrite to land directory.
Example: http://testdomain1.com/?la=template1 - set /lands/template1/ as root dir

As mentioned in my comment, you probably shouldn't be rewriting to a directory. It is not the directory that handles the request, but ultimately a file. By rewriting to a directory I assume you are relying on mod_dir issuing an internal subrequest for the directory index document (as specified by the DirectoryIndex directive). For the sake of this example, I will assume the directory index is index.php.

This would need to go before the above rewrite, since it is more specific:

RewriteCond %{QUERY_STRING} ^la=([\w-]+)
RewriteRule ^$ /lands/%1/index.php [L]

%1 (as opposed to $1) is a backreference to the captured pattern in the last matched CondPattern. The regex [\w-]+ restricts the la parameter value to the characters 0-9, a-z, A-Z, _ and -. The la parameter must also be the first URL parameter.

Upvotes: 1

Related Questions