我有一個帶有子目錄的 HTML 網站。
我需要的是:
- 訪問沒有斜杠的子目錄,即。(
example.com/blog) 不是 (example.com/blog/) - 如果
index.html在某個子目錄下不存在,則應查找index.php。
/blog/頁面是一個 WordPress 頁面,有自己的.htaccess.
這是我當前的根.htaccess:
RewriteEngine on
ErrorDocument 404 /error/error404.html
DirectorySlash Off
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}/index.html -f
RewriteRule ^(.*)$ /$1/index.html [L]
WordPress.htaccess在/blog/.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
(這是由 Wordpress 自動生成的)
至于現在,html檔案可以在某些子目錄下訪問,但不能通過index.php訪問。我該怎么辦?
uj5u.com熱心網友回復:
該DirectoryIndex檔案列出了請求目錄時要嘗試提供的檔案。為此,您不需要 mod_rewrite。
但是,要使用DirectoryIndex設定的時間,DirectorySlash Off您需要使用內部重寫手動將尾隨附加到目錄,否則,即使該目錄包含DirectoryIndex檔案,也會觸發 403 。(正是出于這個原因,需要禁用 mod_autoindex 以防止意外泄露資訊 - 設定時,DirectoryIndex檔案的存在不足以阻止自動生成的目錄串列DirectorySlash Off。)
例如:
# Disable directory listings (mod_autoindex)
Options -Indexes
ErrorDocument 404 /error/error404.html
# Look for index.html then index.php in the directory being requested
DirectoryIndex index.html index.php
# Prevent mod_dir appending the trailing slash
DirectorySlash Off
RewriteEngine On
# If request a directory without a trailing slash then rewrite to append it
# This allows DirectoryIndex to work as intended
# - exclude the document root
RewriteCond $1 !/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (. ) $1/ [L]
確保清除瀏覽器快取。mod_dir 發出 301(永久)重定向以附加尾部斜杠。
更新#1:
/blog/頁面是一個 WordPress 頁面,有自己的.htaccess.
這樣做的問題是,當在子目錄中啟用重寫引擎時,它會完全覆寫父配置中的 mod_rewrite 指令,因此不會附加斜杠并且不會觸發目錄索引(并且不會處理 mod_rewrite 指令,因為缺少的斜線)。(這感覺有點雞和蛋。)
我成功解決此問題的唯一方法是將 WordPress 指令移動到父配置中(進行必要的更改)并/blog/.htaccess完全洗掉。
例如,在上述指令之后附加以下修改后的 WordPress 指令:
# WordPress in "/blog" subdirectory
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^blog/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/. /blog/index.php [L]
更新#2:
是否有帶有尾部斜杠的 URL 重定向到沒有尾部斜杠的 URL?例如,如果我訪問
/about/它應該重定向到/about?
Yes, we can do this. Since we are removing the trailing slash from everything, including directories, this is essentially unconditional. The only caveat is that the rule to remove the trailing slash must only apply to direct requests from the client and not rewritten requests by the later rewrite, that appends the trailing slash to directories, which would otherwise result in a redirect loop.
Aside: You obviously need to ensure that all your internal links do not include the trailing slash, otherwise, the user will experience an external redirect (bad for SEO and your server).
We can do this by checking against the REDIRECT_STATUS environment variable, which is empty on the initial request, and set to 200 (as in 200 OK status) after the first successful rewrite (that appends the trailing slash to directories).
For example, the following should go immediately after the RewriteEngine directive, before the existing rewrite:
# Redirect direct requests to remove trailing slash from all URLs
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (. )/$ /$1 [R=302,L]
Test first with a 302 (temporary) redirect and change to a 301 (permanent) redirect once you have confirmed it works as intended. This is to avoid potential caching issues.
Summary
With all the directives in place:
# Disable directory listings (mod_autoindex)
Options -Indexes
ErrorDocument 404 /error/error404.html
# Look for index.html then index.php in the directory being requested
DirectoryIndex index.html index.php
# Prevent mod_dir appending the trailing slash
DirectorySlash Off
RewriteEngine On
# Redirect direct requests to remove trailing slash from all URLs
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (. )/$ /$1 [R=301,L]
# If request a directory without a trailing slash then rewrite to append it
# - This allows DirectoryIndex to work as intended
# - Exclude the document root
RewriteCond $1 !/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (. ) $1/ [L]
# WordPress in "/blog" subdirectory
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^blog/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/. /blog/index.php [L]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/322379.html
下一篇:使用.htaccess重定向頁面
