1

Haven't been able to find what I'm looking for in .htaccess QA's on here so forgive me if this is duplicate. I have the following problem with a static HTML site. Structure is below.

My relative paths in info.html and contact.html have to use /home/css/style.css while index.html uses /css/style.css. Thanks for help.

Root
- .htaccess
↳ Drupal
  - .git
↳ Dev
  - .git
↳ Home
  - .htaccess
  - .git
  - css
  - js
  - img
  - info.html
  - contact.html
  - index.html

Goal

  • Dev.example.com to use all the files from /Dev
  • example.com to use all the files from example.com/home
  • example.com/info to redirect from example.com/home/info.html.
  • example.com/contact to redirect from example.com/home/contact.html.

What I've tried so far

in root .htaccess

      # Set a default home directory, (this subfolder always loads)
  # RewriteEngine On
  # RewriteCond %{THE_REQUEST} ^GET\ /home/
  # # RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
  # RewriteRule ^home/(.*) /$1 [L,R=301]

  # RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
  # RewriteRule !^home/ home%{REQUEST_URI} [L]

  Options -Indexes

  ErrorDocument 404 /missing.html

  # compress text, html, javascript, css, xml:
  AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4.0[678] no-gzip
  BrowserMatch bMSIE !no-gzip !gzip-only-text/html

  <FilesMatch "\.(htm|html|css|js)$">
  AddDefaultCharset UTF-8
  </FilesMatch>

  RedirectMatch 404 "(?:.*)/(?:\.git|file_or_dir)(?:/.*)?$"

  # remove .html from html file names
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
  RewriteCond %{REQUEST_FILENAME}.html -f
  RewriteRule ^(.+)/$ $1.html [L]

And in Root/drupal .htaccess

default drupal stuff

Closest answers I could find on SO:


EDIT: I ended up following, partially, the advice given because I have separate git repos per subdirectory. Thanks @Jon Lin and @tttony!

New problem: My .htaccess in root doesn't let me view my drupal installation in subfolder.

  • Drupal.examples.com uses all the drupal files from /Drupal**
Community
  • 1
  • 1
Danger14
  • 760
  • 2
  • 12
  • 33

2 Answers2

2

The easiest thing to do, by far, is just make home your document root and not have to worry about having a subdirectory.

But for what you have to work, the first thing you need to do is route everything to /home and not just the / request:

RewriteEngine On
RewriteRule ^(.*)$ /home/$1 [L]

Secondly, this condition:

RewriteCond %{REQUEST_FILENAME}.html -f

will always fail if you have a trailing slash, since it'll try to check for files that look like this:

/home/contact/.html

Try this instead:

# remove .html from html file names
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_URI} ^/(.*)/$
RewriteCond %{DOCUMENT_ROOT}/home/%1.html -f
RewriteRule ^(.+)/$ $1.html [L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Added more details to my folder structure. Will this affect your answer? – Danger14 Oct 14 '13 at 17:38
  • @Danger14 don't think so – Jon Lin Oct 14 '13 at 17:43
  • I can't access any subdomains (Staging.example.com maps to Root/Staging) now. How do I edit my .htaccess in root to allow access to subdomains? – Danger14 Oct 15 '13 at 19:31
  • 1
    @Danger14 try adding either conditions to exclude the subdomain directories (e.g. `RewriteCond %{REQUEST_URI} !^/(staging|other_subdomain|subdomain2)`) or you can add !-f and !-d checks against `%{REQUEST_FILENAME}`. The conditions need to go above the `RewriteRule ^(.+)/$ $1.html [L]` rule. – Jon Lin Oct 15 '13 at 19:51
2

I've had that issues with sub-folders, you can try use RewriteBase in your root .htaccess

RewriteEngine On
RewriteBase /home/

But I found that it's better to use just one .htaccess file in the root folder

# remove .html from html file names
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI} [L,R=301]
RewriteCond home/%{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ home/$1.html [L]
tttony
  • 4,944
  • 4
  • 26
  • 41