You will have 6 answers in 3 minutes.
I have a static site served via nginx, but I don't like seeing .html in the URLs.
It's a little tricky serving a static site without the .html extension because you may have a directory and an html page with the same name.
The way to deal with that is to actually use the .html extension in the file system but not URLs.
location / {
if ($uri ~ ^/google) {
break;
}
if ($uri ~ ^/y_key_) {
break;
}
if ($uri ~ \.html$) {
return 404;
}
if ($uri = /index) {
return 404;
}
if (!-f $request_filename) {
rewrite ^/$ /index.html break;
rewrite .* $uri.html break;
}
}Wouldn't something like this accomplish the same thing? (Sorry if I'm totally wrong, I'm not really good)
location /google/ {
}
location /y_key_/ {
}
location ~ \.html$ {
return 404;
}
location = /index {
return 404;
}
location / {
try_files $uri @rewrite
}
location @rewrite {
rewrite ^/$ /index.html break;
rewrite .* $uri.html break;
}Writing clean URLs is much easier with try_files, which allows you to do something like
try_files $uri.html
If you don't want to see file extensions.We've found good people through odesk to take on tasks we weren't good at -- front-end javascript, document translation, audio transcription, to name a few.
foo.com/index > foo.com foo.com/ > foo.com foo.com/folder/index > foo.com/folder/ foo.com/bar.html > foo.com/bar foo.com/bar.htm > foo.com/bar foo.com/bar.php > foo.com/bar www.foo.com/ANY OF THE ABOVE TESTS > foo.com/*
other rules: Never add trailing slash unless it is an index file of a directory. Order: .PHP,.HTML,.HTM. Use h5bp/server-configs-nginx as a base. PHP-FPM should be fully supported and not exploitable by the foo.com/random.gif.php bug. 403 and 404 send to /404.html.
I am sure I am forgetting something, but that is a start.
# - Remove trailing slashes (except root /)
# e.g. /foo/bar/ -> /foo/bar
# ([^^] matches every character but the start of the string)
rewrite [^^](.*)/$ /$1 permanent;
# - Remove .html and .htm extensions
# e.g. /foo/bar.htm -> /foo/bar
rewrite ^(.*)\.html?$ $1 permanent;
# - Remove index file URLs
# e.g. /foo/bar/index -> /foo/bar/
# (the trailing slash is then removed by the first rewrite)
rewrite ^(.*/)index$ $1 permanent;
Edit: There's a couple extra directives that go along with the above to make it work that I neglected to include: # Try the request URI, and a potential index file in the URI (in the case of a directory).
# This lets you hit the file WEBROOT/foo/bar/index with the URI /foo/bar,
# and hit the file WEBROOT/foo/bar.html with the URI /foo/bar
try_files $uri $uri/index $uri.html =404;
# You need this (or some other way to provide the type information)
# if you don't have extensions on your files.
default_type text/html;
Also, for error pages, you can probably just use the error_page directive: error_page 404 403 /404; # The .html in your spec will be stripped off by the above rewrite rule if ($request_uri ~* "^(.*)\.html?$") {
return 301 $1;
}
The above is a safe use of "if", and can be helpful since it operates on the actual uri, not the internal uri.Also as far as foo.com/ -> foo.com, I was referring to the URL, not the acutal path.
example: https://www.google.com/ redirects to https://www.google.com correct? Maybe I am missing something...
$ curl -I https://www.google.com/
HTTP/1.1 200 OKhttp://www.foo.com/ is the "correct" URL. Remember how HTTP works -- you connect to foo.com port 80, then "GET / HTTP/1.1". You can't just omit the "/" and expect it to work.
HTTP clients will just request "/" if no path is specified, so nginx will never even see a request that matches "^foo.com$". You'll cause an infinite redirect loop if you try to force the issue.