的.htaccess如何根URL重定向到子目录中的文件,重写干净的URL并不会影响子域?重写、重定向、干净、文件

2023-09-02 01:07:04 作者:蹲在墙头调戏红杏

一直没能找到我要找的在.htaccess QA的东西就在这里,所以原谅我,如果这是重复的。的,我有以下问题,一个静态的HTML网站。结构如下。

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.

info.html里我的相对路径和 contact.html 必须使用 /家用/ CSS / style.css中,而 index.html的使用 /css/style.css 。感谢帮助。

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

目标

Dev.example.com 来使用来自所有文件 /开发 example.com 来使用来自所有文件 example.com/home example.com/info example.com/home/info.html 重定向。 example.com/contact example.com/home/contact.html 重定向。 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.

我到目前为止已经试过

在根目录的.htaccess

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]

和根/的Drupal的.htaccess

And in Root/drupal .htaccess

default drupal stuff

最近的答案,我能找到这样:

.htaccess改写重定向根URL子目录 changing我所有的网站链接,最好的方法是什么? Redirecting站点根目录使用到子目录的.htaccess htaccess的子目录的URL根 .htaccess rewrite to redirect root URL to subdirectory changing all my website links , the best way? Redirecting root of site to subdirectory using .htaccess htaccess subdirectory URLS to root

编辑:我结束了之后,部分中,给出的建议,因为我有每个子目录单独的git回购协议。感谢@乔恩林@tttony!

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

新的问题:我的根的.htaccess不让我查看我的Drupal安装在子文件夹

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

Drupal.examples.com 使用所有的Drupal文件从 / Drupal的 ** Drupal.examples.com uses all the drupal files from /Drupal**

推荐答案

做最简单的事情,到目前为止,仅仅是让家居您的文档根目录,而不必担心有一个子目录。

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]