php .htaccess RewriteRule 还重写了我的 css、js 和图像文件。如何忽略这些?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27923450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.htaccess RewriteRule also rewriting my css, js and images files. how to ignore these?
提问by shubh jaiswal
I want that when user input http://example.com/abc/123/jon/in URL is actually show this file http://example.com/abc.php?id=123&name=jonand this is easily can be done by adding these lines into htaccess file:
我希望当用户在 URL 中输入http://example.com/abc/123/jon/实际上显示这个文件http://example.com/abc.php?id=123&name=jon这很容易通过将这些行添加到 htaccess 文件中来完成:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^abc/(.*)/(.*)/ abc.php?id=&name= [L]
but my abc.php file having some (css, js) files:
但是我的 abc.php 文件有一些(css、js)文件:
<link href="css/style.css" rel="stylesheet">
<script src="js/jquery-1.10.2.js"></script>
and images like : img/logo.png
和图像如:img/logo.png
but because of my .htaccess file it is searching for style.css here : http://example.com/abc/123/jon/css/style.cssinstead of http://example.com/css/style.cssand similarly for js and images.
但由于我的 .htaccess 文件,它在这里搜索 style.css:http: //example.com/abc/123/jon/css/style.css而不是http://example.com/css/style.css对于 js 和图像也是如此。
i already tried many similar answers here but none of them working for me.i am looking for .htaccess solutions for this.
我已经在这里尝试了许多类似的答案,但没有一个适合我。我正在为此寻找 .htaccess 解决方案。
回答by Croises
You can use that:
你可以使用它:
RewriteEngine on
# not rewrite css, js and images
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^abc/(.+)/(.+)/ abc.php?id=&name= [L]
# rewrite css, js and images, from root
RewriteRule ^abc/[^/]+/[^/]+/(.+)$ [L]
If your final css (or js) directory is not at root, you can add the path before the last $1
. Now it's: /css/style.css
with /abc/xxx/yyy
/css/style.css
如果您的最终 css(或 js)目录不在根目录下,您可以在最后一个$1
. 现在是:/css/style.css
使用/abc/xxx/yyy
/css/style.css
回答by Forien
Just add before the RewriteRule
one line:
只需在RewriteRule
一行之前添加:
RewriteCond %{REQUEST_FILENAME} !-f
This means that if your request uri points at existing file, then next RewriteRule
will not trigger.
这意味着如果您的请求 uri 指向现有文件,则 nextRewriteRule
不会触发。
If you want other way then this one should do:
如果你想要其他方式,那么这个应该做:
RewriteCond %{REQUEST_FILENAME} !\.(css|js|png|jpg)$
You can add more extensions separated by pole (|
)
您可以添加更多以极 ( |
)分隔的扩展名
回答by shubh jaiswal
Answer by @Croises is correct but i have to add my full domain_url before $1
@Croises 的回答是正确的,但我必须在 $1 之前添加我的完整 domain_url
so final Ans is:
所以最后的答案是:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^abc/(.*)/(.*)/ abc.php?id=&name= [L]
RewriteRule ^abc/[^/]+/[^/]+(/.+)$ http://example.com/ [L]