URL 재 작성 규칙을 사용하여 관리자 디렉터리가 리디렉션되지 않도록 방지

Oct 30 2020

아래 설정은 매우 잘 작동하지만 이동하려고 하면 WordPress 웹 사이트에 로그인 할 수 없기 때문에 원하는대로 https://example.com/quotes/wp-admin리디렉션 https://example.com/wp-admin되지 않습니다 /quotes/.

나는 이것을 따옴표에 추가하려고 시도했지만 .htaccess도움이되지 않습니다.

RewriteCond %{REQUEST_URI} !^/(wp-admin/.*)$

또한 폴더 의 .htaccess파일에 다음 줄을 추가하려고 시도했지만 wp-admin작동하지 않았습니다.

RewriteEngine Off

루트 .htaccess파일 :

# Rewrite any URLs that contain a language code prefix to the subdirectory
RewriteRule ^[a-z]{2}/ quotes%{REQUEST_URI} [L]


# Rewrite any request for a static resource that does not exist (in the root)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(css|js|png|jpg|webp|gif|svg|ttf|woff2)$ quotes%{REQUEST_URI} [L]


# BEGIN rlrssslReallySimpleSSL rsssl_version[3.3.5]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>


# END WordPress

/quotes .htaccess 파일

# Redirect any direct requests for "/quotes/<anything>" back to root
# Except for static resources
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|webp|gif|svg|ttf|woff)$ RewriteRule (.*) /$1 [R=301,L]


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /quotes/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /quotes/index.php [L]
</IfModule>
# END WordPress

답변

2 MrWhite Oct 30 2020 at 17:00

나는 이것을 따옴표에 추가하려고 시도했지만 .htaccess도움이되지 않습니다.

RewriteCond %{REQUEST_URI} !^/(wp-admin/.*)$

REQUEST_URI이 있어야한다, 그래서 서버 변수가 전체 URL 경로가 들어 있습니다 /quotes/wp-admin(즉, 정규 표현식. !^/quotes/wp-admin- 후행 슬래시는 의도적으로 생략). 결과적으로 위의 예외가 실패하고 리디렉션이 계속 발생합니다.

.php피할 확장 목록에 추가 할 수도 있습니다 . 또는 페이지 URL에 "파일 확장자"가 없다고 가정하고 단순히 파일 확장자가있는 것처럼 보이는 모든 요청을 제외 할 수도 있습니다.

예를 들면 다음과 /quotes/.htaccess같습니다.

# Redirect any direct requests for "/quotes/<anything>" back to root
# Except for wp-admin and static resources
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/quotes/wp-admin
RewriteCond %{REQUEST_URI} !\.\w{2,4}$ RewriteRule (.*) /$1 [R=302,L]

\.\w{2,4}$- 이것은 해당 파일 확장 범위에서 2 사이의 4 자 다음에 점으로 구성되어 가정 a-z, A-Z, 0-9또는 _(밑줄)와 URL 경로의 끝. \w해당 문자 범위를 나타내는 속기 문자 클래스입니다.

또한,이 단계를 더 받아 실제 파일에 매핑이 리디렉션되고 있음을 모든 요청을 방지 할 수 있지만 대해 위의 예외 !^/quotes/wp-admin/!\.\w{2,4}$해야 이미 캐치 . 예를 들면 :

# Redirect any direct requests for "/quotes/<anything>" back to root
# Except for wp-admin, static resources and any other files
RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{REQUEST_URI} !^/quotes/wp-admin RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /$1 [R=302,L]

파일 시스템 검사 (예 :) -f는 상대적으로 "비용이 많이 들기 때문에"엄격하게 필요하지 않은 경우 피하는 것이 가장 좋습니다.

또한 wp-admin 폴더의 .htaccess 파일에 다음 줄을 추가하려고 시도했지만 작동하지 않았습니다.

RewriteEngine Off

최소한에서 트리거 한 리디렉션을 방지하려면이 작업이 수행되어야합니다 .htaccess. (이렇게하면 재 작성도 막을 수 있으므로 바람직하지 않을 수 있습니까?)

그러나 이것은 301 (영구) 리디렉션이기 때문에 브라우저에 의해 (영구적으로) 캐시됩니다. 따라서 테스트하기 전에 브라우저 캐시가 지워 졌는지 확인해야합니다.

잠재적 인 캐싱 문제를 방지하기 위해 의도 한대로 모두 작동 할 때까지 302 (임시) 리디렉션으로 테스트합니다.


참고 : 루트 .htaccess파일에서 :

# Rewrite any request for a static resource that does not exist (in the root)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(css|js|png|jpg|webp|gif|svg|ttf|woff2)$ quotes%{REQUEST_URI} [L]

사소한 점,하지만 당신은 참조 woff2여기에,하지만 woff당신의 /quotes/.htaccess파일. 이상적으로는 동일해야합니다. /quotes/.htaccess파일에 대해 위에서 언급했듯이 이것을 일반화 할 수 있습니다. 예를 들면 :

# Rewrite any request for a static resource that does not exist (in the root)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.\w{2,4}$ quotes%{REQUEST_URI} [L]