i would like to rewrite js and css files using nginx
i have this urls pattern
css :
http://myhost.com/css/min/css_home.1330004285.css
js :
http://myhost.com/js/min/js_home.1330004285.js
for the css files have to redirect to
http://myhost.com/css/min/css_home.css and the same way for the js files
i tried to resolve this by using thi solution but i doesn't work , it showing me an error when restarting nginx server
location ~* \.(css|js) {
rewrite /(.*)\.[\d]{10}\.(css|js) $1.$2 last;
}
The rewrite rule seems a bit over complicated.
You can try this:
rewrite /(.+/)\.+\.(css|js)$ /$1.$2 last;
If you have to use your original one, you need to wrap it in quotes because it includes curly braces ... '{' and '}'
rewrite "/(.+)\.[\d]{10}\.(css|js)$" /$1.$2 last;
Related
I try to make specific rewrite rules by .htaccess but i need help, its doesn't work.
This is my url :
http://mywebsite.net/aaa-bbbb/cccc
aaa = dynamic parameter ( [a-z] )
bbb = dynamic parameter ( [a-z] )
cccc = dynamic parameter but optionnal ( [a-z] )
And i want rewrite user to this url :
http://mywebsite.net/mydir/ccc?x=aaa&y=bbbb**
url still will http://mywebsite.net/aaa-bbbb/cccc on tabbar
i try this
RewriteRule ^([a-z]+)-([a-z]+)/([a-z]+)$ mydir/$3 [L]
but it doesn't work
thx !
To rewrite a request to
http://mywebsite.net/aaa-bbbb/cccc
internally to
/mydir/ccc?x=aaa&y=bbbb
you need such a rewrite rule:
RewriteEngine on
RewriteRule ^([a-z]+)-([a-z]+)/([a-z]+)$ /mydir/$3?x=$1&y=$2 [L]
Note: the above rule has the syntax to be used inside the http servers host configuration. If you really want to use a .htaccess style file instead you need to use a slightly modified syntax, since those files work on relative paths:
RewriteEngine on
RewriteRule ^([a-z]+)-([a-z]+)/([a-z]+)$ mydir/$3?x=$1&y=$2 [L]
You can spot the missing / in the rewrite goal. The .htaccess style file has to reside inside the folder holding the mydir folder for this to work. Also the interpretation of such files has to be enabled at all in the http server configuration for.
A general note: .htaccess style files are notoriously error prone, hard to debug and they really slow down the http server for nothing. They are only offered as a last option for those not having access to the http servers host configuration. So for example for cheat shared hosting providers. In general you should always prefer to place such rules inside the host configuration itself.
So I wanted to create a rewrite rule such that when a uses types in
mydomain.com/toplevel/secondlevel/
it rewrites it to
mydomain.com/toplevel/secondlevel
similarly if I have
mydomain.com/toplevel/
I wanted this to be
mydomain.com/toplevel
I understand that this can be done using the rewrite command and regex, but can't seem to find the right regex for it.
Add this rule to main server section:
rewrite ^/(.*)/$ /$1 permanent;
It removes slash at the end from any adress which contains it.
I want to make the following url in nginx
comments.php?id=34
becomes
/comments/34
/comments/34/
I am trying with this and it works
rewrite ^/comments/$id/(.*)$ /comments.php?id=$1? last;
My Question is, how to do I force redirect comments.php?id=x to /comments/id
rewrite ^/comments.php$ /comments/$arg_id? permanent;
According to the documentation, "rewrite operates only on path, not parameters."
Try this instead:
if ($args ~ id=(.+)){
rewrite comments\.php /comments/$1 last;
}
I am trying to rewrite some urls with following nginx rewrite rule
rewrite ^/some\/url\/(.*)\/$ /some/url/?filter=$1;
Rewriting does not seem to work if a query contains an & e.g.
?filter=key:abcd & efgh
which I am quoting properly to
?filter=abcd%20%26%20N%20efgh
My problem is only rewritten url /som/url/key:abcd%20%26%20N%20efgh do not work. If I access it like /some/url/?filter=abcd%20%26%20N%20efgh it works fine.
Am I missing something?
Thanks.
Putting URL you want to rewrite to another "location" could solve your problem.
(I suppose in location directive nginx doesn't do any escaping)
So for your example it would be:
location /some/url/ {
if ($uri ~* ^/some/url/(.*)$
{
rewrite ^.*$ /some/url/?filter=$1 break;
}
proxy_pass http://127.0.0.1:8080;
#proxy_set... <- and other proxy related things
}
at least it worked for me(in my case even question marks were replaced with "%3f".
Excerpt from official documentation "Note that the $args variable is not decoded, unlike URIs during location matching." http://wiki.nginx.org/HttpRewriteModule
I noticed that I was using an old version of the nginx(7.6) and the problem was solved by upgrading to the latest stable release(1.0).
HI,
Given the following apache/mod_rewrite rule taken from .htaccess within minify directory on the server:
RewriteRule ^([a-z]=.*) index.php?$1 [L,NE]
what will be the nginx compatible equivalent of it? I have tried:
rewrite ^/minify/([a-z]=.*) minify/index.php?$1 break;
but that doesn't seem to work.
Any ideas guys?
I have something like this and it's working for me
rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
Try something more like bbPress's code.