.htaccess 두 번째가 있으면 첫 번째 PHP URL 매개변수를 제거합니다.

.htaccess 두 번째가 있으면 첫 번째 PHP URL 매개변수를 제거합니다.

이것은 내 .htaccess 코드입니다.

RewriteCond %{REQUEST_URI} !^/pages/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/([^/]+))? pages.php?PAGE=$1&LINK=$3 [L]

*$1 = 프로필, $3 = John-smith

이것은 다음과 같이 잘 다시 작성됩니다.https://example.com/profile/john-smith하지만 다음과 같은 두 번째 재작성 규칙이 필요합니다.https://example.com/john-smithjohn-smith를 포함하는 두 번째 매개변수가 존재하는 경우에만 가능합니다.

감사합니다!

업데이트: (내 .htaccess 파일의 전체 규칙)

# protect files beginning with .
RewriteRule /\.(.*) - [NC,F]

# redirect HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

# No root access without index.* and other security
RewriteEngine On
Options All -Indexes
RewriteBase /
DirectoryIndex index.php index.html index.htm
ErrorDocument 404 https://example.com/pages.php?PAGE=404

# Prevent upload malicious PHP files
<FilesMatch “\.(php|php\.)$”> 
Order Allow,Deny 
Deny from all 
</FilesMatch>

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

RewriteCond %{REQUEST_URI} !^/pages/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/([^/]+))? pages.php?PAGE=$1&LINK=$3 [QSA, L]

답변1

RewriteRule ^([^/]+)(/([^/]+))? pages.php?PAGE=$1&LINK=$3 [L]

이에 대한 "문제"는 (두 번째 그룹은 선택 사항)에 대한 요청과도 일치 하지만 필수가 아닌 에 /john-smith대한 요청을 다시 작성한다는 것입니다 . 이를 위해서는 별도의 규칙이 필요합니다. 또한 일치하여 삭제 하지만 잠재적으로 스패머에게 사이트를 공개하는 요청(다대일 관계)을 다시 작성합니다.pages.php?PAGE=john-smith&LINK=pages.php?LINK=john-smith/profile/john-smith/anything/anything

.예에 따라 URL 경로 세그먼트에 점( )을 허용하지 않는다고 가정하면 요청이 파일에 매핑되지 않는지 확인할 필요가 없습니다. 예. 파일이 모두 파일 확장자를 갖는 경우 요청은 /profile/john-smith파일에 매핑될 수 없으므로 파일 시스템 검사가 중복됩니다.

대신 다음을 시도해 보세요.

# Rewrite exactly one path segment
# eg. /john-smith to pages.php?LINK=john-smith
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)$ pages.php?LINK=$1 [L]

# Rewrite exactly two path segments
# eg. /profile/john-smith to pages.php?PAGE=profile&LINK=john-smith
RewriteCond %{REQUEST_URI} !^/pages/ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/([^/.]+)$ pages.php?PAGE=$1&LINK=$2 [L]

NC이전 지시문의 플래그는 아마도 RewriteCond중복될 수 있습니다.

([^/.]+)- 도트도 제외하도록 캡처 하위 패턴을 변경했습니다. 두 번째 규칙은 하나가 아닌 정확히 두 개의 경로 세그먼트와 일치합니다.또는귀하의 예에서와 같이 두 개의 경로 세그먼트.

관련 정보