Php .htacces将资产重定向到资产文件夹

Php .htacces将资产重定向到资产文件夹,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,我有一个PHP站点设置,每个站点都有一个文件夹(例如Login),其中有一个Index.PHP文件和站点特定资产。但是,每个页面所需的一些资产存储在直接位于最高级别下的“资产”文件夹中(这有意义吗?) 我使用了.htaccess并获得了此代码 RewriteEngine on RewriteBase / RewriteRule ^(.*)$ http://janberktold.com/Assets/$1 [R=301,L] 然而,我的问题是:它重定向 localhost/Login/test

我有一个PHP站点设置,每个站点都有一个文件夹(例如Login),其中有一个Index.PHP文件和站点特定资产。但是,每个页面所需的一些资产存储在直接位于最高级别下的“资产”文件夹中(这有意义吗?)

我使用了.htaccess并获得了此代码

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://janberktold.com/Assets/$1 [R=301,L]
然而,我的问题是:它重定向

localhost/Login/test.css

localhost/Assets/Login/test.css

而不是

localhost/Assets/test.css

如何让服务器重定向到正确的路径?

试试这个

RewriteEngine on
RewriteBase /

# Rewrite if the file does not exists
RewriteCond %{REQUEST_FILENAME} !-f

# Rewrite only if the URI does not starts with Assets
RewriteCond %{REQUEST_URI} !^/Assets

# Rewrite any assets file
RewriteRule ([^/]*).(css|js|png|jpe?g)$ Assets/$1.$2 [L]
这应该将所有资产文件
localhost/dir/file.css
localhost/dir/dir2/file.css
重写为
localhost/assets/file.css

试试这个

RewriteEngine on
RewriteBase /

# Rewrite if the file does not exists
RewriteCond %{REQUEST_FILENAME} !-f

# Rewrite only if the URI does not starts with Assets
RewriteCond %{REQUEST_URI} !^/Assets

# Rewrite any assets file
RewriteRule ([^/]*).(css|js|png|jpe?g)$ Assets/$1.$2 [L]

这应该将所有资产文件
localhost/dir/file.css
localhost/dir/dir2/file.css
重写为
localhost/assets/file.css

将规则替换为:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:[^/]*/)*([^/.]+\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /Assets/$1 [R=301,L,NC]

将规则替换为:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:[^/]*/)*([^/.]+\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /Assets/$1 [R=301,L,NC]

尝试将
/Assets/test.css
作为链接url并删除.htaccess。像
@imsiso一样,我知道这个解决方案,但是我希望能够只放置“test.css”并被重定向。可能只是为了知道如何做。请检查我的答案。为什么需要使用301重定向?@tttony这不是必需的,我只是使用了它,因为我看到了一个示例,使用ittry
/Assets/test.css
作为链接url并删除.htaccess。像
@imsiso一样,我知道这个解决方案,但是我希望能够只放置“test.css”并被重定向。可能只是为了知道如何做。请检查我的答案。为什么需要使用301重定向?@tttony这不是必需的,我只是使用了它,因为我看到了一个使用它+1的示例,很好,但是其他资产呢?有多扩展支持的解决方案怎么样?+我的答案是什么?我有一个相反的情况,我的资产文件夹路径是相对的,如img src=“..../../assets/img.png”,在这种方式下,我不能编辑代码,我唯一能做的就是从htaccess文件重写。我的根文件夹是/abc。你能给我一个重定向规则,提到任何东西吗?/../assets/img.png就像/abc/assets/img.png+1一样好,但是其他的资产呢?有多扩展支持的解决方案怎么样?+我的答案是什么?我有一个相反的情况,我的资产文件夹路径是相对的,如img src=“..../../assets/img.png”,在这种方式下,我不能编辑代码,我唯一能做的就是从htaccess文件重写。我的根文件夹是/abc。你能给我一个重定向规则吗?/./…assets/img.png类似于/abc/assets/img.png