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

时间:2022-02-23 00:25:55

I have my PHP site setup with a folder for each site (e.g Login), which has an Index.php file and site-specfic assets. However, some of the assets, which are required by every single page, are stored in a 'Assets' folder located directly under the highest level (does that make sense?).

我的PHP站点设置有每个站点的文件夹(e)。g Login),具有索引。php文件和站点特定资产。但是,每个页面都需要一些资产,它们被存储在直接位于*别的“assets”文件夹中(这有意义吗?)

I have played around with .htaccess and got this code

我使用了。htaccess,得到了这段代码

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://janberktold.com/Assets/$1 [R=301,L]

However, my problem is: It redirects

然而,我的问题是:它重定向

localhost/Login/test.css

localhost /登录/ test.css

to

localhost/Assets/Login/test.css

/登录/ test.css localhost /资产

instead of

而不是

localhost/Assets/test.css

localhost /资产/ test.css

How do I get my server to redirect to the correct path?

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

2 个解决方案

#1


1  

Replace your rules with:

取代你的规则:

RewriteEngine on

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

#2


2  

Try this

试试这个

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]

This should rewrite any assets files localhost/dir/file.css or localhost/dir/dir2/file.css to localhost/Assets/file.css

这应该重写任何资产文件localhost/dir/文件。css或localhost / dir / dir2 /文件。css localhost /资产/ file.css

#1


1  

Replace your rules with:

取代你的规则:

RewriteEngine on

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

#2


2  

Try this

试试这个

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]

This should rewrite any assets files localhost/dir/file.css or localhost/dir/dir2/file.css to localhost/Assets/file.css

这应该重写任何资产文件localhost/dir/文件。css或localhost / dir / dir2 /文件。css localhost /资产/ file.css