4

我试图让我的 htaccess 文件不重写我的静态文件(js/css/images)。

这是我当前的 htaccess 文件:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule    !\.(jpg|css|js|gif|png)$    public/    [L]
RewriteRule !\.(jpg|css|js|gif|png)$ public/index.php?url=$1

我该如何重写它?

4

3 回答 3

14
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ public/index.php?url=$1

所有对不以列出的扩展名结尾的不存在文件的请求(不区分大小写的匹配)都被重写为public/index.php将当前 URL 作为url=GET 参数传递

于 2012-11-18T11:44:15.820 回答
6

这仅适用于它下面的一行。因此,如果您有许多 RewriteRules,则使用以下内容可能会有所帮助:

    RewriteEngine on

    RewriteRule ^(.*?)\.(php|css|js|jpg|jpeg|png|pdf)$ - [L]
    RewriteRule ^(.+)/(.+)/?$ index.php?page=$1&subpage=$2 [L]
    RewriteRule ^(.+)$ index.php?page=$1 [L]
于 2015-04-09T21:16:00.623 回答
2

为后人。

也许我的规则会有用

\www\.htaccess

#file: \www\.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1?a [QSA,L]

RewriteCond %{REQUEST_URI} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?) [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1 [QSA,L]

#RUN TEST
RewriteCond %{REQUEST_URI} !\.(css|js|html?|png|gif|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule ^(.*)$ index.php?REQUEST_FILENAME=%{REQUEST_FILENAME}&date=$1&REQUEST_URI=%{REQUEST_URI} [QSA,L]
#END TEST

RewriteRule . index.php?url=%{REQUEST_URI} [QSA,L]

ErrorDocument 404 index.php?error=404

\www\index.php

<?php
// file: /www/index.php
 var_dump($_GET);

\www\app\.htaccess

#file: \www\app\.htaccess`

 RewriteEngine On
 RewriteRule ^(.*)$ ../_%{REQUEST_URI} [QSA,L]

\www\public\.htaccess

#file: \www\public\.htacces`
 RewriteEngine on

 RewriteCond %{REQUEST_FILENAME} !-l 
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f

 RewriteRule ^(.*)$ index.php?url=%{REQUEST_URI}&rewrite=$1 [QSA,L]

/www/public/index.php

<?php
// file: /www/public/index.php
 var_dump($_GET, __DIR__);

于 2021-10-10T11:23:59.540 回答