【问题标题】:.htaccess How to redirect root URL to subdirectory files, rewrite to clean URL AND not affect subdomains?.htaccess 如何将根 URL 重定向到子目录文件,重写为干净的 URL 并且不影响子域?
【发布时间】:2013-10-21 15:18:47
【问题描述】:

无法在此处的 .htaccess QA 中找到我要查找的内容,如果重复,请见谅。我在静态 HTML 网站上遇到以下问题。结构如下。

我在info.htmlcontact.html 中的相对路径必须使用/home/css/style.css,而index.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 重定向。

到目前为止我所做的尝试

在根 .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 存储库。谢谢@Jon Lin 和@tttony!

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

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

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    到目前为止,最简单的做法是将 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]
    

    【讨论】:

    • 向我的文件夹结构添加了更多详细信息。这会影响您的回答吗?
    • @Danger14 不这么认为
    • 我现在无法访问任何子域(Staging.example.com 映射到 Root/Staging)。如何在 root 中编辑我的 .htaccess 以允许访问子域?
    • @Danger14 尝试添加任一条件以排除子域目录(例如 RewriteCond %{REQUEST_URI} !^/(staging|other_subdomain|subdomain2)),或者您可以添加 !-f 和 !-d 检查 %{REQUEST_FILENAME}。条件需要超过RewriteRule ^(.+)/$ $1.html [L]规则。
    【解决方案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]
    

    【讨论】:

      猜你喜欢
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      相关资源
      最近更新 更多