I need
/secure/token/timestamp/path/to/the/resource/
to be rewritten in this format:
/s/path/to/the/resource/?st=token&e=timestamp
what I'm trying to do is:
location /secure/(?<token>)/(?<timestamp>)/(?<path>) {
rewrite ^ /s/$path/?st=$token&e=$timestamp
}
but it's obviously not working. This syntax doesn't seemed to be correct. How can I play like this with urls in nginx rules?
EDIT:
trying now something like this: the same result:
location /secure/(.*)/(.*)/(.*) {
rewrite ^ /s/$3?st=$1token&e=$2timestamp;
}
You have multiple problems. You have confused prefix locations with regex locations. Your named captures do not contain any content. And your numeric captures are probably too broad.
See this for location syntax.
You should probably use a prefix location, then capture in the rewrite:
location /secure/ {
rewrite ^/secure/([^/]*)/([^/]*)/(.*)$ /s/$3?st=$1&e=$2 last;
}
Related
I saw an nginx rewrite rule that captured the query parameters from the URL and applied the same query parameters to the rewritten URL. I can understand that in some circumstances it could/would be beneficial to capture the query parameters in the rewrite rule, but it seems to me that in this situation doing so is pointless.
The rewrite rule in question is as follows:
rewrite ^/users/(\d+)\.html(?:\?(.*))?$ /users/profile.php?id=$1&$2 last;
Is this pointless as I suspect? I believe that the following rewrite rule would have the same effect:
rewrite ^/users/(\d+)\.html$ /users/profile.php?id=$1 last;
The first rewrite won't work, $2 will always be empty, even if there's a query string that's passed to the request,
your second rewrite is quite correct but I don't think it would preserve the query string, if you want to do that then you should append it by your self.
rewrite ^/users/(\d+)\.html$ /users/profile.php?id=$1&$query_string last;
I want to redirect all these kind of requests
http://www.example.com/2013/01/my-sample-post/feed
or
http://www.example.com/2013/01/my-sample-post/feed/
to
http://www.example.com/2013/01/my-sample-post/
I am using sinatra and rack--rewrite gem.
rewrite %r{/*/feed?}, '/$1' // not working..
You missed a dot in the regex. You also need a capture group with () around the wildcard, so it should be (.*) between the slashes. Iguess you are also missing a slash at the end. This should work:
rewrite %r{/(.*)/feed/?}, '/$1'
I'm using mod Rewrite to rewrite part of the URL to a query parameter
URLs are of the form
http://www.my_site.com/ABC
where ABC is the bit to force into a query parameter to my index.php in a sub directory
My .htaccess has the following:
RewriteEngine on
RewriteRule ^/?([a-zA-Z0-9]+)/?$ directory/index.php?Site=$1 [L]
When I have a lowercase ABC e.g:
my_site.com/abc
the browser URL remains unchanged and the page works perfectly with abc being passed in the GET.
However, when ABC is UPPERCASE e.g:
my_site.com/ABC
the URL in the browser becomes:
my_site.com/ABC/?Site=ABC
It works OK and ABC is still in the GET parameter but it looks ugly!
Why does case affect this and can I make BOTH upper and lower case maintain the original URL?
Further note: I tried an example of a different server and it worked perfectly! Any ideas if there are server settings that could affect this?
Thanks for any assistance
I've solved it - as sidoh said something else is going on.
If there is a directory on my server which is the same name as the parameter the mod_rewrite adds the query to the URL.
e.g.
If I've got a directory ABC on the server then using my mod_rewrite with www.domain.com/ABC results in the browser URL www.domain.com/ABC/Site=ABC - if no directory ABC all is fine and the URL remains www.domain.com/ABC (which is what I want).
Since I'm on Linux ABC is different to abc - which explains the apparent case sensitive nature of the behaviour.
Thanks to all for their inputs.
Do you mean, the [NC] nocase flag? Which will make the rule be matched in a case-insensitive manner. Means, it will doesn't care whether letters appear as upper-case or lower-case.
RewriteRule ^([a-zA-Z0-9]+)/?$ /directory/index.php?Site=$1 [NC]
I think, you want to rewrite a URL such /bigSMALL into /directory/index.php?Site=BIGsmall? If am I right, then the problem is in the program and not in URL remapping. However, in MySQL database, when you're getting the value of a query string for querying a WHERE value, it is match in a case-insensitive. Take a look at the picture:
Are you developing a website? If ever, then what DBMS are you using? Maybe, MySQL?
I cannot wrap my head around URL rewriting. What I want to do seems very simple but I am having problems getting the results I want.
I would like allow users to type www.mysite.com/search/real with an optional / at the end. This would take them to www.mysite.com/content/search_real_property.asp
That's it. Here is the rule I have right now. The problem with this is it will keep stacking.
RewriteRule ^(search) content/search_real_property.asp
So this would work /search/real but so would search/real/search/real/search/real/
and others.
Assuming there are no other issues, you've turned the rewrite engine on (RewriteEngine On) and that you're either adding the rewrite in httpd-vhosts.conf or an .htaccess file in the root of the web tree (so that any path issues are resolved)... then the issue is merely one of Regular Expression pattern matching. Though I'm a bit perplexed by ASP running on what appears to be an Apache server (assuming this IS mod rewrite we're talking about).
So, all you really want is to terminate the match - something like:
RewriteEngine On
RewriteRule ^search/real/?$ /content/search_real_property.asp
That will fix it to /search/real (with or without a trailing slash, the ? means match the preceding character 0 or 1 times) to /content/search_real_property.asp. As the $ sign denotes the line terminator (EOL effectively) there must be nothing after "real" (except perhaps that 1 forward slash).
For greater flexibility you might want to look at what you can actually do with regular expressions, for instance...
RewriteEngine On
RewriteRule ^search/([^/]*)/?$ /content/search_real_property.asp?query=$1
Which would allow you to take any string and pass it in the address bar as a variable called query (Request.QueryString('query') IIRC).
Try: http://www.regular-expressions.info/ for more info.
I am using below rule to read the URL like
URL:
http://www.example.com/blog/sampe-post-title/10004/
RULE:
RewriteRule (.*)/(.*)/([0-9]+)/$ $1/details.asp?mod_id=$3 [NS,I]
Everything was fine untill I discovered that links coming via feedburner are not working anymore. Because feedburner adds some extra parameter to URL for stats/tracking etc.
For example www.example.com/blog/sampe-post-title/10004/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed:+somesite+(my+feed)
My rewrite URL doesn't recognizes the above URL anymore. Any idea how to deal with it?
Try adding a rule for the feedburner URLs:
RewriteRule (.*)/(.*)/([0-9]+)/\?(.*)$ $1/details.asp?mod_id=$3&$4 [NS,I]
I added an extra RegEx group at the end to capture everything after the question mark and place it after mod_id. You could probably combine this with your other URL if you only wanted to have one rule for some reason, but you might as well just have two.
Try the QSA flag to get the original requested URL query automatically appended to the new one.
I managed to figure out the answer on my own: just replace the ending $ with .* That's it!
Here is the modified rule:
RewriteRule (.*)/(.*)/([0-9]+)/.* $1/details.asp?mod_id=$3 [NS,I]