2016-02-25 53 views
0

我試圖弄清楚如何在每個url的末尾添加「/」。 而這個網址需要沒有擴展名。 例如:添加斜槓並剪切文件擴展名(nginx規則)

example.com/about.php; example.com/about.html => example.com/about/ 

我假裝配置nginx重寫規則。 這是我的配置:

server{ 
    root /usr/share/nginx/www/example.com; 
    index index.php index.html index.htm; 

    # Make site accessible from http://localhost/ 
    server_name example.com www.example.com; 

    client_max_body_size 20M; 

    # BEGIN W3TC Browser Cache 
    gzip on; 
    gzip_types text/css text/x-component application/x-javascript application/json application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text$; 
    # END W3TC Browser Cache 

    location /blog { 
      # First attempt to serve request as file, then 
      # as directory, then fall back to index.html 
      # try_files $uri $uri/ /index.php; 
      try_files $uri $uri/ /blog/index.php?q=$uri&$args; 
      # Uncomment to enable naxsi on this location 
      # include /etc/nginx/naxsi.rules 
    } 



    location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css|json)$ { 
      expires max; 
    } 

    location ~ \.php$ { 
      try_files $uri =404; 
      fastcgi_split_path_info ^(.+\.php)(/.+)$; 
      fastcgi_pass unix:/var/run/php5-fpm.sock; 
      fastcgi_index index.php; 
      include fastcgi_params; 
    } 

    location/{ 
     try_files $uri $uri.html $uri/ @extensionless-php; 
     index index.php index.html index.htm; 
    } 

    location @extensionless-php { 
     rewrite ^(.*)$ $1.php last; 
    } 

} 

我創辦了一些解決方案,但我不能把它結合起來,因爲我在nginx的小白。

+0

[This answer](http://serverfault.com/questions/754144/nginx-rewrite-reset-rewrite-or-add-extensions-to-a- specifics目錄/ 754311#754311)可能幫你。 –

+0

@RichardSmith謝謝你的回答!試圖執行... – BordiArt

+0

@RichardSmith同樣的問題 – BordiArt

回答

1

你需要這樣的

location/{ 

         rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent; 
         rewrite ^(/.*[^/])(?!/)$ $1/ permanent; 


         try_files $uri $uri.html $uri/ @extensionless-php; 
         index index.php; 

       } 

       location @extensionless-php { 
        rewrite ^(.*)/$ $1.php last; 
       } 

第一行rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent;更改配置的一部分,將重定向與擴展的所有請求的.html或.PHP

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

將添加/來請求,URI沒有/

相關問題