I have categories I'd like to rewrite.
for example:
example.com/videos?c=18
to:
example.com/category/name
I tried to do this using multiple examples, most had no effect and this example gave me only page not found to all /videos pages:
location /videos {
if ($args ~ "c=18") {
rewrite ^/videos(.*) http://$server_name/category/name$1 permanent;
}
}
Is this even doable purely via Nginx what I am trying to achieve?
As far as I got the question, currently your site has this url scheme, which you can't change:
http://example.com/videos?c=18
But you would like to present visitors with "pretty"-looking URLs like
http://example.com/category/name
That pretty URL does not really exist anywhere on the site, which is why you have to rewrite it, e.g. turn pretty virtual url into a real one that your scripts can process.
Once again, you rewrite from virtual to actual, not the other way round.
The following directive would turn /category/cars/ into /videos?c=cars
location /category {
rewrite ^/category/(.*)$ /videos?c=$1 last;
}
But your script won't understand /videos?c=cars url, it needs category ID to work. So in your case the pretty url should look like
http://example.com/category/18
which will be rewritten to
http://example.com/videos?c=18
"if" directive is not best solution, but in your case you can try "if" and $arg_name (argument name in the request line):
location /videos {
if ($arg_c = "18") {
rewrite ^/videos(.*) http://$server_name/category/cars? permanent;
}
if ($arg_c = "19") {
rewrite ^/videos(.*) http://$server_name/category/bikes? permanent;
}
# and so on
}
Related
I need to redirect all URLs formatted as http://www.example.org/dir/subdir/ to http://subdomain.example.org/subdir/. For example, http://www.example.org/dir/subdir/page would redirect to http://subdomain.example.org/subdir/page.
I tried rewrite ^(/dir/subdir.*) http://subdomain.example.org$1 permanent;, but this keeps the /dir/ part, which I want to omit.
Your rewrite rule is capturing the /dir/ part too.
Try this:
location /dir/subdir {
rewrite ^/dir(.*)$ http://subdomain.example.org$1 permanent;
}
I need to enter a bunch or rewrites in my conf file in Nginx. I am not very experienced so I copied what I found before, example.
location = /index.php/blog/blog/xxx/yyy/ {
return 301 /index.php/blog/xxx/yyy/;
}
However I was told that the best way is the following:
location ^~ /index.php/blog/blog/xxx/yyy/ {
rewrite ^/index.php/blog/xxx/yyy/;
}
Which one id the correct one?
The first one is more correct, both location as well as the return -wise, and it'll work faster.
FWIIW, your second snippet looks like it's missing a space in the rewrite after ^, and it's also less efficient, both location as well as rewrite-wise.
References:
http://nginx.org/r/location
http://nginx.org/r/return
http://nginx.org/r/rewrite
We have several SEO pages like:
http://www.example.com/PageOne.html
Which we redirect in config like:
location = /PageOne.html {
rewrite ^/(.*) /seo.php?id=1 last;
}
Problem is if a user access this page by typing:
http://www.example.com/pageone.html
"Page Not Found" error is displaying. There are approximate 500+ seo pages. How to write rule for nginx to ignore case sensitivity in url? I want a common solution for all url.
Specifically for PageOne.html, you can do the following:
location ~ /PageOne.html {
return 301 http://www.example.com/pageone.html$1;
}
If you have multiple URIs which need to be redirected, it appears the best option is to use Perl:
location ~ [A-Z] {
perl 'sub { my $r = shift; $r->internal_redirect(lc($r->uri)); }';
}
If you have hundreds of unique URIs which would involve many location blocks as above, I'd consider changing your application to handle lowercase URIs rather than expecting the webserver to handle the lowercase conversion.
This solved my issue. Sad to say that there is not many articles related to these issues, even nginx doesn't provide user friendly Help/Tutorials.
location ~* ^/-PageOne.html {
rewrite ^ /seo.php?page_id=1 last;
}
Hope this helps!
I have a rule which is not working correctly.
I need it so that whenever URL xxx.com/forum/css.php is hit, it is re-written to xxx.com/forum/core/css.php.
I have written the following location block for it:
location ~^ /forum/css.php {
rewrite ^ /forum/core/css.php permanent;
}
Also needing to be taken into account is that the file is a factory so it accepts parameters, the url being hit actually looks like xxx.com/forum/css.php?x=123&y=string. Will this also be taken into account in the re-writes or does it need to be specified? Sorry if the question seems silly I am just beginning to work with servers! Thanks fellow coders!
To rewrite a single URI (with or without query string) you could use a location =:
location = /forum/css.php {
rewrite ^ /forum/core/css.php permanent;
}
The rewrite directive appends the query string (unless terminated with ?). See this and this for more.
I'm trying to set up a permanent redirect from
http://domain.com/member/blog_post_view.php?postId=1
to
http://blog.domain.com/friendly-url-here
The source URL contains both a ? and an = which I think might be the cause but am unsure.
I've tried all sorts of nginx suggestiosn including the one below but can't seem to get the redirection to work and hoped someone can point me in the right direction.
location /blog_post_view.php?postId=1 {
rewrite "/blog_post_view.php\?postId\=1" http://blog.domain.com/friendly-url-here permanent;
}
That part of request line starting from the question mark is called query string, while the location directive matches only path part of URI.
You should use $arg_* variable instead:
location =/blog_post_view.php {
if ($arg_postId = 1) {
return 301 http://blog.domain.com/friendly-url-here;
}
}
Reference:
http://nginx.org/r/location
http://nginx.org/r/if
Embedded Variables