问题
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.comto use all the files from/Devexample.comto use all the files fromexample.com/homeexample.com/infoto redirect fromexample.com/home/info.html.example.com/contactto redirect fromexample.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:
- .htaccess rewrite to redirect root URL to subdirectory
- Changing all my website links: what is the best way?
- Redirecting root of site to subdirectory using .htaccess
- htaccess subdirectory URLS to root
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.comuses all the drupal files from/Drupal**
回答1:
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]
回答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]
来源:https://stackoverflow.com/questions/19353788/htaccess-how-to-redirect-root-url-to-subdirectory-files-rewrite-to-clean-url-a