我正在尝試通過htaccess檔案重寫URL,以便以下URL
www.domain.com/subfolder/index.php?key
可以通過以下方式訪問:
www.domain.com/subfolder/index.php/key
指定的"键"將確定要包含在PHP代碼中的頁面。 我已经有以下htaccess代碼,但是使用第二个(干净)URL時,CSS,JS,圖像等未顯示.關於可能的問题有什麼想法吗?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
RewriteRule ^index.php/([a-zA-Z0-9/_]+)$ index.php?key=$1
最新回復
- 5月前1 #
- 5月前2 #
實際上,.htaccess有一个解決方案!
假設您的index.php位於
/mysite/
中 而您的資源檔案在/mysite/scripts/
中 ,/mysite/images/
安德/mysite/style/
像地雷一樣,你可以這樣:RewriteEngine on #first exclude those folders RewriteBase "/mysite/" RewriteRule ^(style|scripts|images)($|/) - [L] #then go with your rules (the [L] terminates the ruling) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteCond %{DOCUMENT_ROOT}/$1 -f RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC] RewriteRule ^index.php/([a-zA-Z0-9/_]+)$ index.php?key=$1
您可以看到.htaccess mod_rewrite-如何以其他几種方式从重寫規則中排除目錄
- 5月前3 #
此外,您可以尝試將其添加到HTaccess中:
RewriteBase /
相似問題
- apache:如何忽略URL末尾的URLapachehtaccessubuntuurlmodrewrite2021-01-02 06:27
- .htaccess:Apache:服務器配置拒绝客戶端apachehtaccess2021-01-10 12:24
- .htaccess:ht-access如何重定向到論坛主题URL?htaccessurlredirectmodrewriteurlrewriting2021-01-03 03:23
- apache:htaccess重寫任何檔案夹,但不重寫任何檔案apachehtaccessmodrewriteurlrewriting2020-12-29 18:28
- apache:帶破折號或连字元的RewriteRuleapachehtaccessmodrewriteurlrewriting2020-12-29 20:24
当您使用相對URL時,瀏覽器將通過使用載入的資源的URL動態建立一个完整的URL.換句话說:它使用網址欄中顯示的網址.就您而言(
www.domain.com/subfolder/index.php/key
)它尝試載入相對於www.domain.com/subfolder/index.php/
的任何相對URL .但是,您的資源不在此處。您有两種選擇来解決此問题:
Convert your relative url's into absolute url's, at least absolute to the domain root. Something like
<img src="img/unicorn.png">
應该變成<img src="/path/to/img/unicorn.png">
為您的head元素添加一个底脚.將使用此基础而不是資源的網址来計算完整的網址.您應该添加
p<base href="/">
给你的<head>
元件./
然後將用作任何相對URL的基础。您尝試使用基本URL吗? 那是我遇到的最好的解決方案.只需將一个放在頭上,然後您甚至可以给它一个ID並使用javascript訪問URL.
使用jquery訪問