簡體   English   中英

如何使用 .htaccess 文件重寫子目錄的 URL?

[英]How can I use the .htaccess file to rewrite URL's for a subdirectory?

我目前正在使用它來使我的 URL 看起來很漂亮:

ErrorDocument 403 /404.php
ErrorDocument 404 /404.php
Options -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

我終於讓它工作了(我對此很陌生),但現在我需要它來為位於主站點子目錄中的“子站點”工作。 Instead of rewriting https://example.com/index.php it needs to rewrite https://example.com/sub/index.php

我嘗試了一些事情,比如更改 RewriteBase 和 RewriteRule,但沒有成功。

可以肯定的是,我應該可以將另一個 .htaccess 放在子目錄中,並期望它重寫 example.com/sub 的 URL,對嗎?

提前致謝!

編輯我向主 .htaccess 添加了例外,如下所示: ErrorDocument 403 /404.php ErrorDocument 404 /404.php Options -Indexes

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(/controle|/controle/|/voting|/voting/.*)$
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

但是,我仍然不知道將什么放入子目錄中的 .htaccess 中,同樣的一個不起作用,我嘗試將RewriteBase /更改為RewriteBase /subdir1/

可以肯定的是,我應該可以將另一個.htaccess放在子目錄中,並期望它重寫example.com/sub的 URL,對嗎?

是的。 默認情況下,/ .htaccess /subdir/.htaccess中的 mod_rewrite 指令。 但是,您將需要重新定義ErrorDocument指令以覆蓋根。

 RewriteCond %{REQUEST_FILENAME}/index.html.-f RewriteCond %{REQUEST_FILENAME}/index.php !-f

澄清一下,在這兩個條件下,您是否希望偶爾從請求的子目錄提供index.htmlindex.php (DirectoryIndex) 文檔,而不是將請求路由到文檔根目錄中的/index.php 在這種情況下,簡單地檢查請求是否不是 map 到目錄(即RewriteCond %{REQUEST_FILENAME} !-d )會更常見(並且稍微更優)。 除非有一些物理子目錄要路由到文檔根目錄中的/index.php

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME}.-f RewriteCond %{REQUEST_FILENAME}/index.html.-f RewriteCond %{REQUEST_FILENAME}/index.php !-f RewriteRule . index.php [L] </IfModule>

這些指令可以稍微簡化 - 如果您想將相同的指令應用於/subdir中的子站點(並改為提供/subdir/index.php ),這將對您有所幫助。

完全刪除RewriteBase指令。 相對替代字符串,即。 index.php ,然后將相對於.htaccess文件所在的目錄(或繼承)。

您也不需要<IfModule mod_rewrite.c>包裝器。 有關更多信息,請參閱我對 Webmasters Stack 上以下問題的回答https://webmasters.stackexchange.com/questions/112600/is-checking-for-mod-write-really-necessary

所以,上面可以寫成:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]

(請注意我上面關於用目錄檢查替換這兩個條件的評論。)

現在,您可以簡單地將完全相同的指令復制到/subdir/.htaccess文件中,這些指令現在會將請求路由到/subdir/index.php ,而不是覆蓋父項中的 mod_rewrite 指令。

無需復制Options -Indexes ,除非您想更改此選項。

或者,如果您想應用完全相同的指令(但與/subdir相關),您只需在/subdir/.htaccess文件中啟用 mod_rewrite inheritance 即可。 例如:

# /subdir/.htaccess

ErrorDocument 403 /subdir/404.php
ErrorDocument 404 /subdir/404.php

RewriteEngine On
RewriteOptions Inherit

RewriteOptions Inherit的作用本質上是從父配置中“復制” mod_rewrite 指令,即。 /.htaccess在根目錄中,位於當前/subdir/.htaccess文件中的指令后面。 重要的是要澄清指令是“復制的”,它們不像在根/.htaccess文件中那樣就地運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM