2017-03-15 61 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