簡體   English   中英

htaccess多個條件並重寫

[英]htaccess multiple conditions and rewrites

我正在嘗試處理以下內容:

  1. 所有網址都訪問www.mydomain.com/index.php
  2. GET參數仍在處理中
  3. 刪除.php

    因此,如果我有:www.mydomain.com/test.php?a=1我想在這里檢查文件app / controllers / test.php是否存在,並將字符串app / controllers / text.php傳遞到./index .php並從test.php中刪除.php

    要么

    如果我有:www.mydomain.com/dir1/test.php?a=1我想在這里檢查文件app / dir1 / controllers / test.php是否存在,並傳遞字符串app / dir1 / controllers / text。 php到./index.php並從test.php刪除.php

    要么

    如果我有:www.mydomain.com/dir1/dir2/dir3/test.php?a=1我想在這里檢查文件app / dir1 / dir2 / dir3 / controllers / test.php是否存在並傳遞字符串app / dir1 / dir2 / dir3 / controllers / text.php到./index.php並從test.php中刪除.php

如您所見,sub dir必須是動態的,因此DIR和FILE檢查是必需的。

所以..如果它在任何目錄級別都存在php文件test.php

  1. 從瀏覽器網址中刪除.php並將參數發送到www.mydomain.com/index.php,以便我可以選擇文件的路徑,但瀏覽器從未看到文件擴展名

如果不存在(我已經在這部分工作了),我想繼續並運行htaccess中已有的內容。 這將在目錄路徑上使用index.php,如果找不到則重定向到404

我希望它看起來像這樣:

Options +FollowSymlinks
RewriteEngine On

#Rewrite Conditions to detect actual file at path adding app/controllers 
#to front of url string as this shouldn't be viewable
# -- CODE I AM STUCK WORKING OUT --
# [L] to prevent proceeding with htaccess if condition met and rewrite done

# Existing code below to run if above not found
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ ./index.php?p=$1 [L,NC,QSA]

謝謝

d

要從URL本身剝離.php ,您需要重定向:

RewriteRule (.*)\.php$ $1 [QSA,R=301]

現在應用您的實際邏輯,我認為這應該起作用:

RewriteRule (.*)/([^/]+)$ index.php?file=app/$1/controllers/$2.php [QSA]

然后,您將不得不讓PHP檢查if( file_exists($_GET['file'])) ,因為您的條件太復雜而無法檢入.htaccess (將controllers/插入要檢查的路徑)

暫無
暫無

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

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