A given wordpress site had once a given link http://www.website.com/link/ which existed but now doesn't exist and I need to redirect it (301) to an existing one: http://www.website.com/link-existing/
I tried a lot of things which doesn't work:
location /link/ {
try_files $uri $uri/ /link-existing/;
}
location /link/ {
return 301 /link-existing/;
}
Related
I have my main site at x.com (# /var/www/x.com/index.html)
# MAIN LOCATION
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#autoindex on;
proxy_set_header X-Real-IP $remote_addr;
}
I want /v2 to redirect to another local dir (# /var/www/x.com/v2/public/index.html)
# Redirect beta site
location /v2 {
alias /var/www/x.com/v2/public;
}
This seems like it should work, but instead, it's going to my 404 site. Not sure if this matters, but here is my root above both of them:
# path
root /var/www/throneoflies.com/html;
I tried ordering "/v2" both above "/" and below - didn't seem to make a difference. I read I shouldn't use 'root' instead of 'alias' because it's a different schema (/v2/public/ and not just /v2/).
EDIT: Seems like it should be working - I've read a lot since this post. Here is my full file:
server {
# MAIN >>
# SSL configuration
listen 443 default_server ssl;
server_name www.x.com;
error_page 404 /error/404.html;
server_tokens off;
# SSL
ssl_certificate /etc/ssl/x.com/cert.pem;
ssl_certificate_key /etc/ssl/x.com/key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_session_tickets off;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
# path
root /var/www/x.com/html;
#root /var/www/x.com/v2/public; #works!
# Add index.php to the list if you are using PHP
#index index.php index.html index.htm index.nginx-debian.html;
index index.html;
# 404 handling - no cache
location = /404.html {
add_header Cache-Control "no-cache" always;
}
# Redirect beta site : Why doesn't it work if the outer block root changed to the same path works? I can REPLACE my "/" with this v2 path np. However, alias at /v2 does not work.
location = /v2 {
alias /var/www/x.com/v2/public;
#root /var/www/x.com/v2/public;
#try_files $uri $uri/ =404;
}
# MAIN LOCATION
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#autoindex on;
proxy_set_header X-Real-IP $remote_addr;
# DENY .HTACCESS ACCESS
location ~/\.ht {
deny all;
}
}
This should obviously be caused by the = in your location = /v2, which will only match requests where the $uri is exactly /v2; the correct way would most likely be to remove =.
Additionally, note that if using location /v2 {alias /var/www/x.com/v2/public;}, then folks would also be able to access things like /var/www/x.com/v2/public.bak through /v2.bak; if that's not intentional, then you would be better off having trailing slashes, as appropriate.
I want to redirect an old path to a new one without changing the url and keeping the suffix of the url for the redirection:
dev-osm.blah.com/NYC/v2/site/links?key=a12345
redirect to
dev-osm.blah.com/NYC/v1/site/links2?key=a12345
server {
server_name dev-osm.blah.com
.
.
location ^~ /NYC/v2/site/links {
rewrite ^ /NYC/v1/site/links2
}
Use something like the following in your server config
location /NYC/v2
proxy_pass: /NYC/v1/
I am tearing my hair out on this one while configuring some rewrite rules on nginx. I am running nginx 1.2.1 on a Debian Wheezy machine.
Considering the following tree:
/
├── index.html
├── folder/
│ └── index.html
└── file.html
I would like those files to be served by nginx so that there would be no need to specify any .html in the URL, and that any call to a folder/index.html is rewritten in folder/, any call to file.html is rewritten in index.
Here is the formal version of this description. Request examples are on the left and the corresponding response I am trying to get are on the right (HTTP code 301 + redirected location or HTTP code 200 + file to be displayed):
1. http://example.com/ -> 200 (/index.html)
2. http://example.com/index.html -> 301 http://example.com/
3. http://example.com/index -> 301 http://example.com/
4. http://example.com/file -> 200 (/file.html)
5. http://example.com/file.html -> 301 http://example.com/file
6. http://example.com/folder/ -> 200 (/folder/index.html)
7. http://example.com/folder -> 301 http://example.com/folder/
8. http://example.com/folder/index.html -> 301 http://example.com/folder/
9. http://example.com/folder/index -> 301 http://example.com/folder/
10. http://example.com/foobar -> 404
So far, my closest attempt is the following configuration of /etc/nginx/sites-enabled/example.com:
server {
server_name example.com;
root /var/www/example.com/public_html;
rewrite ^(.*/)index(\.html)*$ $1 permanent; # Applies to 2, 3, 8, 9
rewrite ^(/.+)\.html$ $1 permanent; # Applies to 5
try_files $uri $uri.html "${uri}index.html" =404; # Handles 1, 4, 6, 10
}
As you can see, case 7 is missing. For now, I got:
7'. http://example.com/folder -> 404
I also managed to have:
7''. http://example.com/folder -> 200 (/folder/index.html)
But it is definitely something I don't want SEO wise (among others), as 2 different URLs (with and without trailing slash) returns the same content.
Every configuration did not pass all of my test cases.
Note that when I simply disable the 2 rewrites and the try_files, 7 applies as expected (as well as 1, 6, 10 but not the other ones), the redirection is valid by default. I don't understand how and where this redirection rule, so here are my formal questions: how can I make this redirection re-appear and, by extension, how can I have my 10 test cases properly working?
Thanks a lot, and I really mean it :)
PS: I try my best, but of course if there is anything unclear, do not hesitate to ask for clarification!
You got a couple of things wrong, first you won't do any 301 redirects, redirects are using if you are accessing something, but then you want it to go to something else, like for example if we're doing an upgrade.
http://example.com/index.html =>redirects to=> http://example.com/maintenance.html
What you need is a changing the format of something to something else, your rewrites are swapped by the way, rewrites work like this
rewrite [what to match] [what to do with it]
Your rewrites are saying when I get a URL to /index.html it will redirect to /index which then will give you a 404 because you don't have a real file called /index
I'd like to do it with try_files
server {
server_name example.com www.example.com;
root /my/root/path;
location / {
try_files $uri $uri/ $uri.html;
}
Try this and tell me how it goes.
I'm trying to configure Nginx on a new server. I have a number of PHP scripts (f.e. /test/test.php) and I want to use this scripts "as is" (default language, English), as well as with language redirecting. Example - when "/de/test/test.php" is requested,
nginx writes a cookie (lang=de)
and returns "/test/test.php" (without modifying URI, so that visitor remains on "/de/test/test.php"
Any help is greatly appreciated! I already lost several nights fighting with this, and I'm getting desperate enough to cancel new server and return back to shared hosting.
Thanks!
Create a sub location under your php location:
location ~ ^.+\.php$ {
# Return '400 Bad Request' for malformed URL exploits
location ~ \..*/.*\.php$ {
return 400;
}
location ~ ^/de/(.+)\.php {
add_header Set-Cookie lang=de;
rewrite ^/de/(.+)\.php$ /$1.php last;
}
# Your proxy or fastcgi code to process PHP
...
}
I know it's old but in case anyone else finds it useful, below is how I solved it (with some help from a friend). I set LANG variable in nginx, which I then pick up in PHP $_SERVER['LANG'].
# languages
location ~ ^/(af|sq|ar|hy|az|be|bs|bg|cs|zh|da|de|nl|el|en|et|fa|fi|fr|ka|he|hi|hr|hu|id|it|ja|kk|ko|lv|lt|nb|no|pl|pt|ro|ru|sk|sl|es|sr|sv|th|tk|tr|uk|uz|vi|bp)/
{
set $lang $1;
rewrite ^/[^/]+/(.+)$ /$1 break;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_read_timeout 600;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param LANG $lang;
}
Okay I'm kind of n00b on Nginx, and I've browsed through here and couldn't piece together an answer. SO here is what i got
server {
root /usr/share/nginx/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location /dojump {
rewrite ^/dojump/(.*)$ /dojump/index.php/$1 break;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
This is on a wordpress setup the first location block should pass all file requests to the wordpress bootstrap.
the location /dojump block is supposed to be for an outbound redirect script i have. I want to catch the arguments and pass them to the index.php script
like /dojump/cnn.com
to /dojump/index.php/cnn.com
it works with apache with this simple .htaccess line inside the dojumps folder
RewriteRule ^(.*)$ index.php/$1 [L]
however, I get an nginx error in the error log
/usr/share/nginx/www/dojump/index.php/cnn.com" failed (20: Not a directory)
Any help?
Thanks
Try passing the url as a GET parameter.
rewrite ^/dojump/(.*)$ /dojump/index.php?url=$1 break;