programing

NGINX를 사용하여 URL에서 .php와 .html 확장자를 모두 제거하는 방법은 무엇입니까?

mailnote 2023. 9. 13. 22:53
반응형

NGINX를 사용하여 URL에서 .php와 .html 확장자를 모두 제거하는 방법은 무엇입니까?

나는 nginx가 모든 url을 깨끗하게 표시하기를 원합니다.

몇 가지 조사를 통해 저는 첫 번째 사례를 만들었습니다.다음과 같은 구성에 의해 수행됩니다.

location / {
    root   html;
    index  index.html index.htm index.php;
    try_files $uri.html $uri/ =404; 
}

indexhtml.html을 indexhtml로 표시하는 데는 작동하지만 .php에서는 아무 일도 일어나지 않습니다.$uri.html을 $uri.php로 변경하면 .html에서도 작동하지 않고 .php에서도 작동하지 않습니다.pp 위치에 비슷한 것을 넣으려고 했지만 성공하지 못했습니다.

조언 좀 해주세요.

제가 조사한 바로는 /etc/nginx/conf.d/domain.tld.conf 파일을 추가하면 다음을 포함할 수 있습니다.

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

location ~ \.php$ {
    try_files $uri =404;
}

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

그럼 nginx를 다시 시작해서 한 번 해보세요.이것이 여러분에게 도움이 되기를 바랍니다!자세한 정보는 (찾은 곳에서) here @ tweaktalk.net 에서 확인할 수 있습니다.

별도의 블록과 지명된 장소 및 모든 것이 필요 없습니다.또한 이동합니다.index로케이션 블록 바깥쪽에 줄

server {
  index index.html index.php;
  location / {
    try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
  }
  location ~ \.php$ {
    try_files $uri =404;
    # add fastcgi_pass line here, depending if you use socket or port
  }
}

같은 폴더 안에 같은 이름의 폴더와 파일이 있는 경우 다음과 같이 기억하십시오./folder/xyz/그리고./folder/xyz.php폴더를 사용하면 php 파일을 실행할 수 없습니다.xyz포함.index.php아니면index.html, 이 점만 명심하세요.

Mohammad의 답변을 더 자세히 알려면, 당신은 또한 다음으로부터 리다이렉트를 제공할 수 있습니다..html그리고..php확장이 필요 없는 버전으로.

이는 "전체 원래 요청 URI(인수 포함)"가 포함되어 있기 때문에 가능하며, 사용자가 볼 수 없는 내부 재쓰기의 영향을 받지 않습니다.

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
  }
}

이것은 5년이 넘도록 저에게 효과가 있었습니다.

location / {
               
        try_files $uri/ $uri.html $uri.php$is_args$query_string;

}

어쩌면 이것이 당신에게 도움이 될지도...간단하며 작업을 완료할 수 있습니다.

location / {
  rewrite ^/([^\.]+)$ /$1.html break;
}

언급URL : https://stackoverflow.com/questions/21911297/how-to-remove-both-php-and-html-extensions-from-url-using-nginx

반응형