2017-03-15 64 views
0

我最近將一個使用Apache的網站移到了Nginx上。nginx - 重寫php文件的url

我怎樣才能得到一個URL看起來像這樣:

http://example.com/login 

指向http://example.com/login.php,同時保持.PHP出來的url?

我目前的配置:

server { 
    listen 80; 
    listen [::]:80; 

    root /var/www/example.com/html/; 
    index index.php index.html; 

    server_name example.com www.example.com; 

    location/{ 
      try_files $uri $uri/ =404; 
    } 

    # deny accessing php files for the /assets directory 
    location ~ ^/assets/.*\.php$ { 
      deny all; 
    } 

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
    # 
    location ~ \.php$ { 
      include snippets/fastcgi-php.conf; 

      # With php7.0-cgi alone: 
      # fastcgi_pass 127.0.0.1:9000; 
      # With php7.0-fpm: 
      fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    } 
} 

回答

0

我通常使用這樣的事情,它的搜索引擎優化的證明。經過我的許多客戶站點測試,像魅力一樣工作。

在你/etc/nginx/conf.d/domain.tld.conf文件中包含的:

server { 
    index index.html index.php; 
    location/{ 
    if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; } 
    try_files $uri $uri/ $uri.html $uri.php?$args; 
    } 
    location ~ \.php$ { 
    if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 302 /$1?$args; } 
    try_files $uri =404; 
    # add fastcgi_pass line here, depending if you use socket or port 
    } 
} 

Source i used a while a go

+0

謝謝您的回答。在我目前的配置中......它將如何工作?我試過了,但nginx服務器沒有重啓。 – Ciprian