簡體   English   中英

.htaccess不包括重寫css和圖像

[英].htaccess excluding rewrite css and images

我在重寫URL時遇到了一些問題

我的.htacces文件:

RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)$ index.php?url=$1 [NC,QSA,L]

我的文件和文件夾結構:

bsc/
---|.htaccess
---|index.php
---|css/
-------|style.css
---|images/
----------|logo.png
----------|image1.jpg
----------|image2.jpg
---|controller/
--------------|bootstrap.php
...

當我有下一個鏈接

localhost/bsc/page.html

比我的css工作或我可以去上班。 但是當我有下一個鏈接時:

localhost/bsc/page/subpage.html
localhost/bsc/page/subpage/subsubpage.html
localhost/bsc/page/subpage/subsubpage/subsubsubpage.html

我無法讓css工作,css被重定向到

localhost/bsc/page/css/style.css
localhost/bsc/page/subpage/css/style.css
localhost/bsc/page/subpage/subsubpage/css/style.css

問題的第二部分是我的鏈接沒有相同的擴展名或任何擴展名。 下一種情況是可能的:

localhost/bsc/page/subpage
localhost/bsc/page/subpage/
localhost/bsc/page/subpage.php
localhost/bsc/page/subpage.html
localhost/bsc/page/subpage/subsubpage
localhost/bsc/page/subpage/subsubpage/
localhost/bsc/page/subpage/subsubpage.php
localhost/bsc/page/subpage/subsubpage.html
...

現在定義的重寫規則適用於不同的鏈接,但如何為css和圖像添加排除的規則?


我接下來.htaccess適用於所有條件的css:

RewriteEngine On
RewriteBase /bsc/
RewriteCond %{REQUEST_FILENAME} !\.css
RewriteRule ^(.+/?)$ index.php?url=$1 [NC,QSA,L]

閱讀評論后有一段時間我得到了下一個.htaccess文件並沒有那么糟糕的結果:

RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)?(/|\.htm|\.php|\.html|/[^.]*)$ index.php?url=$1 [NC,QSA,L]

和我的鏈接標簽是:

<link rel="stylesheet" type="text/css" href="/bsc/css/style.css" />

現在,當我無法獲得正確的URL請求時,URL中只有一個條件。 下一個URL不接收最后一個參數:

localhost/bsc/page/subpage
localhost/bsc/page/subpage/subsubpage

我沒有收到子頁面 頁面 ,其他條件正在運行。 在這種情況下apache和.htaccess托盤來查找文件或文件夾....

是否可以添加缺失條件,或者是否可以制定一些最簡單的重寫規則或更多規則

您可能只需要為您使用絕對網址,

<link rel="stylesheet" type="text/css" href="/css/style.css">

代替

<link rel="stylesheet" type="text/css" href="css/style.css">

在前面放一個斜線迫使它到網址的“根”,從而使其在整個子頁面中保持一致。

我將url重寫應用於核心php中的項目。 我遇到了同樣的問題。 css,Js和圖像沒有加載。 我用過這個,它對我有用。 資源

# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
  1. 在重寫之前添加條件RewriteCond %{REQUEST_FILENAME} !-f
  2. 你應該包括css與src="/css/style.css"和NOT src="css/style.css" (注意缺少的起始/

暫無
暫無

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

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