2014-12-08 5 views
0

URL 프로필 URL을 깨끗하게 작성하려고합니다. 적어도 사용자에게는 매개 변수가 포함되지 않습니다. 내가 nginx 서버를 사용 하고이 일하는 데 몇 가지 문제가 있습니다.nginx PHP 사용자 프로필 다시 작성

사이트 예 : http://website.com/subfolder/

개요 링크 : 작동 무엇 http://website.com/subfolder/profile.php

: 나는 작업 할 무엇 http://website.com/subfolder/profile.php?username=Bobby

: http://website.com/subfolder/Bobby

내 웹 사이트 설정의 코드 블록 :

location /subfolder/ { 
    if ($query_string ~ "^username=([A-Za-z0-9]+)$"){ 
     rewrite ^/profile.php$ http://website.com/subfolder/%1 redirect; 
    } 
} 
+0

단 하나의 힌트, 당신은 반대로, 당신은 내부적으로 PHP가 이해하지 못할 무언가에 쿼리 문자열로 요청을 재작 성하려고합니다. PHP 코드에 최종 URL을 작성하고 Nginx에서 내부적으로이 새 URL에서 쿼리 문자열 버전으로 재 작성을 관리해야합니다. – regilero

답변

0

귀하의 리디렉션이 거의 정확합니다. 외관상의 변화가 거의 없습니다.

location = /subfolder/profile.php { 
    if ($arg_username) { 
     return 301 /subfolder/$arg_username/; 
    } 
    fastcgi_pass 127.0.0.1:9001; 
    include fastcgi_params; 
} 

이제 regilero 말한 방법, 당신은/하위 폴더/이름/내부에 스크립트를 구문 분석 할 필요하거나

location ~ ^/subfolder/(?<username>.+)/$ { 
    rewrite ^(.*)$ /subfolder/profile.php?username=$username break; 
    fastcgi_pass 127.0.0.1:9001; 
    include fastcgi_params; 
    fastcgi_param REQUEST_URI $uri$is_args$args; 
} 

이 중요한 사항의 nginx하여 작업을 수행 할 수 있습니다 리디렉션 루프에서 보호 break 키워드를 재 작성; fastcgi_param REQUEST_URI은 일반적으로 $request_uri (fastcgi_params)이지만 원래 URI이므로 변경해야합니다.