.htaccess를 사용하여 다른 PHP 페이지에서 index.php와 파일 확장자를 모두 제거합니다.

.htaccess를 사용하여 다른 PHP 페이지에서 index.php와 파일 확장자를 모두 제거합니다.

내 링크에 index.php를 포함할 필요성을 제거하고 싶습니다. 대신에

example.com/about/index.php

그랬으면 좋겠어

example.com/about/

둘째, URL에 .php 파일 확장자가 없어도 색인이 아닌 다른 페이지를 탐색할 수 있기를 바랍니다. 대신에

example.com/about/who_we_are.php

그랬으면 좋겠어

example.com/about/who_we_are

두 가지를 동시에 작동시킬 수 있는 행운이 없습니다. 내 .htaccess 파일에 있는 내용은 다음과 같습니다.

Options -Indexes
DirectoryIndex index.php index.htm index.html
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

현재 비색인 페이지에서 .php를 제거하고 있지만 모든 디렉토리 색인 페이지는 홈페이지로 리디렉션됩니다.

내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변1

이 질문을 게시한 직후에 이 문제에 대한 해결책을 찾았습니다.

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)index$ $1 [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

이것이 이 문제에 대한 해결책을 찾는 다른 사람에게 도움이 되기를 바랍니다.

관련 정보