htaccess斜杠加载资源 #启用重写引擎 重新启动发动机 #删除用户对系统文件夹的访问权限。 重写cond%{REQUEST_URI}^app* 重写规则^(.*)$/index.php?/$1[L] #一些额外条件 #跳过这些路径进行重定向 重写规则^img/(*)公共/资产/img/$1[L] 重写规则^css/(*)public/assets/css/$1[L] 重写规则^js/(*)public/assets/js/$1[L] 重写规则^plugins/(*)public/assets/plugins/$1[L] #检查用户是否试图访问有效文件, #例如图像或css文档,如果这不是真的,它会发送 #请求index.php 重写条件%{REQUEST_URI}!^.*\。(jpg | css | js | gif | png)$[NC] 重写规则^(.*)$public/index.php?url=/$1[QSA,L] #如果我们没有安装mod_rewrite,所有404 #可以发送到index.php,一切正常。 ErrorDocument 404/index.php
这种重写是有效的,只是当我加载像这样的URLhtaccess斜杠加载资源 #启用重写引擎 重新启动发动机 #删除用户对系统文件夹的访问权限。 重写cond%{REQUEST_URI}^app* 重写规则^(.*)$/index.php?/$1[L] #一些额外条件 #跳过这些路径进行重定向 重写规则^img/(*)公共/资产/img/$1[L] 重写规则^css/(*)public/assets/css/$1[L] 重写规则^js/(*)public/assets/js/$1[L] 重写规则^plugins/(*)public/assets/plugins/$1[L] #检查用户是否试图访问有效文件, #例如图像或css文档,如果这不是真的,它会发送 #请求index.php 重写条件%{REQUEST_URI}!^.*\。(jpg | css | js | gif | png)$[NC] 重写规则^(.*)$public/index.php?url=/$1[QSA,L] #如果我们没有安装mod_rewrite,所有404 #可以发送到index.php,一切正常。 ErrorDocument 404/index.php,php,.htaccess,resources,rewriting,Php,.htaccess,Resources,Rewriting,这种重写是有效的,只是当我加载像这样的URLlocalhost/login/(尾随斜杠)时,它没有加载资源。当我使用localhost/login时,它会加载所有图像和CSS文件。有人能帮我解决这个问题吗 这可能是因为您在HTML中使用了相对路径。在URL末尾加上一个斜杠,就客户端URL而言,实际上是在另一个子文件夹中 要解决这个问题,请在客户端代码中使用根相对URL(以斜杠开头)或绝对URL。例如: <IfModule mod_rewrite.c> # Enable rew
localhost/login/
(尾随斜杠)时,它没有加载资源。当我使用localhost/login
时,它会加载所有图像和CSS文件。有人能帮我解决这个问题吗 这可能是因为您在HTML中使用了相对路径。在URL末尾加上一个斜杠,就客户端URL而言,实际上是在另一个子文件夹中
要解决这个问题,请在客户端代码中使用根相对URL(以斜杠开头)或绝对URL。例如:
<IfModule mod_rewrite.c>
# Enable rewrite engine
RewriteEngine On
#Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^app.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Some extra conditions
# skip these paths for redirection
RewriteRule ^img/(.*) public/assets/img/$1 [L]
RewriteRule ^css/(.*) public/assets/css/$1 [L]
RewriteRule ^js/(.*) public/assets/js/$1 [L]
RewriteRule ^plugins/(.*) public/assets/plugins/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ public/index.php?url=/$1 [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</IfModule>
或者,如果您无法修改URL,请在head
部分中包含base
元素,用于指示浏览器相对URL的位置。例如,要使它们相对于文档根:
/img/picture.jpg
或者,通过重定向到.htaccess中的非斜杠URL,或者在
index.php
中处理,防止后面的斜杠返回有效文档。这些类型的问题可能更适合该站点。
<base href="/">