Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add slash to the end of every url (need rewrite rule for nginx)

I try to get an "/" to every urls end:

example.com/art

should

example.com/art/

I use nginx as webserver.

I need the rewrite rule for this..

For better understanding check this:

http://3much.schnickschnack.info/art/projekte

If u press on a small thumbnail under the big picture it reloads and shows this url:

http://3much.schnickschnack.info/art/projekte/#0

If i now have a slash on all urls (on the end) it would work without a reload of the site.

Right now i have this settings in nginx-http.conf:

server {   listen *:80;   server_name 3much.schnickschnack.info;   access_log /data/plone/deamon/var/log/main-plone-access.log;   rewrite ^/(.*)$ /VirtualHostBase/http/3much.schnickschnack.info:80/2much/VirtualHostRoot/$1 last;   location / {     proxy_pass http://cache;   } } 

How do I configure nginx to add a slash? (I think i should a rewrite rule?)

like image 845
Gomez Avatar asked Mar 14 '09 12:03

Gomez


People also ask

How do you add a trailing slash to a URL?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.

How do you write rewrite rules in NGINX?

The syntax of rewrite directive is: rewrite regex replacement-url [flag]; regex: The PCRE based regular expression that will be used to match against incoming request URI. replacement-url: If the regular expression matches against the requested URI then the replacement string is used to change the requested URI.

Why slash is used in URL?

The addition of a slash at the end of a URL instructs the web server to search for a directory. This speeds the web page loading because the server will retrieve the content of the web page without wasting time searching for the file.


1 Answers

More likely I think you would want something like this:

rewrite ^([^.]*[^/])$ $1/ permanent; 

The Regular Expression translates to: "rewrite all URIs without any '.' in them that don't end with a '/' to the URI + '/'" Or simply: "If the URI doesn't have a period and does not end with a slash, add a slash to the end"

The reason for only rewriting URI's without dots in them makes it so any file with a file extension doesn't get rewritten. For example your images, css, javascript, etc and prevent possible redirect loops if using some php framework that does its own rewrites also

Another common rewrite to accompany this would be:

rewrite ^([^.]*)$ /index.php; 

This very simply rewrites all URI's that don't have periods in them to your index.php (or whatever file you would execute your controller from).

like image 172
Bob Monteverde Avatar answered Nov 10 '22 09:11

Bob Monteverde