簡體   English   中英

.htaccess mod_rewrite在主目錄和子目錄中

[英].htaccess mod_rewrite in main directory and subdirectory

我有一個主目錄,其中帶有.htaccess文件,如下所示

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [L]

子目錄中的一個看起來與此相同。 我想要的.htaccess文件要做的是重定向到每個目錄中的索引文件。

所以,如果你訪問主/例如您重定向到主目錄下的index.php文件,但如果你訪問主/子目錄您重定向到子目錄 index.php文件。

我已經找到了答案,但沒有找到任何有效的方法。

感謝您的幫助!

嘗試

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond $1 -d
RewriteRule ^(.+)/ $1/index.php [L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^ index.php [L]
RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/ /$1/index.php [L]

此配置執行以下操作:

如果我們有這個文件

/index.php
/foo/index.php
/var/index.php
/var/boo/index.php

然后您請求關注文件

/foo/hello

您將轉到:

/foo/index.php

並且,如果您要求

/var/boo/world

您將轉到:

/var/boo/index.php

您可以使用這種簡單的解決方案,而無需在任何地方放置.htaccess文件

在主目錄中放一個.htaccess文件,保留代碼

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [L]

然后添加這兩行

  Options All -Indexes
  ErrorDocument 403 http://domain.com/index.php

這兩行阻止用戶訪問該文件夾,並返回狀態403,然后將403狀態重定向到主目錄的index.php地址

暫無
暫無

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

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