无法在.htaccess QA上找到我正在寻找的内容,请原谅我,如果这是重复的。我对静态HTML网站有以下问题。结构如下。
我info.html
和contact.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/info
从example.com/home/info.html
重定向。example.com/contact
从example.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
** 答案 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]