.htaccess如何将根URL重定向到子目录文件,重写以清除URL并且不影响子域?

时间:2013-10-14 05:04:32

标签: apache .htaccess

无法在.htaccess QA上找到我正在寻找的内容,请原谅我,如果这是重复的。我对静态HTML网站有以下问题。结构如下。

info.htmlcontact.html的相对路径必须使用/home/css/style.cssindex.html使用/css/style.css。感谢帮助。

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

目标

  • Dev.example.com使用/Dev
  • 中的所有文件
  • example.com使用example.com/home
  • 中的所有文件
  • example.com/infoexample.com/home/info.html重定向。
  • example.com/contactexample.com/home/contact.html重定向。

到目前为止我尝试了什么

在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]

在Root / drupal .htaccess

default drupal stuff

我能在SO上找到最贴切的答案:


编辑:我最终部分地遵循了给出的建议,因为每个子目录都有单独的git repos。谢谢@Jon Lin和@tttony!

新问题: root中的我的.htaccess不允许我在子文件夹中查看我的drupal安装。

  • Drupal.examples.com使用/Drupal **
  • 中的所有drupal文件

2 个答案:

答案 0 :(得分:2)

到目前为止,最简单的方法就是让你的文档根目录home,而不必担心有一个子目录。

但是,对于您必须工作的内容,您需要做的第一件事就是将所有内容路由到/home而不仅仅是/请求:

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

其次,这个条件:

RewriteCond %{REQUEST_FILENAME}.html -f
如果你有一个尾部斜杠,

总会失败,因为它会尝试检查看起来像这样的文件:

/home/contact/.html

请改为尝试:

# 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]

答案 1 :(得分:2)

我遇到了子文件夹的问题,您可以尝试在根目录中使用RewriteBase .htaccess

RewriteEngine On
RewriteBase /home/

但我发现最好只在根文件夹中使用一个.htaccess文件

# 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]