.htaccess url rewrite remove 2 id

Nov 02 2020

URL의 2 개의 매개 변수 ID를 삭제하고 번호 만 표시 할 수 있습니까? 현재 다음과 같은 URL이 있습니다.

http://localhost/photo_gallery/public/product_configurator.html?prometheus_id=10082&id=73

필요한 URL :

http://localhost/photo_gallery/public/product_configurator.html/10082&73

.htaccess

RewriteEngine on
RewriteRule ^(.*).html $1.php RewriteRule ^product_configurator.php/(\d+)$ product_configurator.php?prometheus_id=$1&id=$2 [NC,L]

그렇게 할 수 있습니까?

답변

MrWhite Nov 02 2020 at 20:03

나는 귀하의 .htaccess파일이 /photo_gallery/public/.htaccess.

RewriteRule ^product_configurator.php/(\d+)$ product_configurator.php?prometheus_id=$1&id=$2 [NC,L]

캡처하는 하위 패턴 (예 (\d+)) 이 하나 뿐이지 만 대체 문자열 에 두 개의 역 참조 (예 : $1$2)가 있습니다. 또한 일치하는 숫자 ( ) 만 일치하는 반면 일치하려는 패턴에는 숫자가 아닌 숫자가 포함됩니다. 따라서 위의 내용은 일치하지 않습니다.\d&

.php확장 프로그램 도 도입 했지만 게시 한 예제 URL은 .html. 나는 당신이 이미 다시 작성하는 있으리라 믿고있어 .html.php(당신의 지시에 의해 제안). 그러나이 작업은 단일 지시문으로 직접 수행해야합니다.

확장자를에서 .html.php(예 :) 변경하기 위해 다시 작성 RewriteRule ^(.*).html $1.php하면 URL 경로에 포함되어 있으므로 일치하려는 정보를 덮어 쓰게됩니다.

대신 다음을 시도하십시오.

RewriteEngine On

# Rewrite "product_configurator.html/10082&73"
RewriteRule ^(product_configurator)\.html/(\d+)&(\d+)$ $1.php?prometheus_id=$2&id=$3 [L]

product_configurator요청에서 ( $1역 참조로) 문자열을 캡처하여 대체 문자열에 반복을 저장 합니다. $2그리고 $3그 이전과 이후 값을 표시 &하는 URL 경로입니다.

NC잠재적 인 "중복 콘텐츠"문제를 방지하기 위해 플래그를 제거했습니다 . 특별히 대소 문자를 구분하지 않는 일치가 필요한 경우가 아니면 NC플래그를 사용하지 않아야합니다.

그리고...

# Rewrite other ".html" requests to ".php"
RewriteRule (.+)\.html$ $1.php [L]