.htaccess 파일을 기준으로 다시 쓰기 규칙을 만드는 방법

.htaccess 파일을 기준으로 다시 쓰기 규칙을 만드는 방법

현재 이와 같은 .htaccess 파일이 있습니다.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/(always|rewrite|these|dirs)/ [NC]

RewriteRule ^(.*)$ router.php [L,QSA]

사이트 파일이 웹 서버의 document_root에 있을 때 생성이 작동합니다(예: domain.com/abc.php-> <DOCUMENT_ROOT>/abc.php). 그러나 현재 설정(변경할 수 없음)에서는 이것이 보장되지 않습니다. 때때로 문서 루트와 .htaccess 파일 폴더 사이에 임의의 폴더가 있을 수 있습니다(예: domain.com/something/abc.php-> <DOCUMENT_ROOT>/something/abc.php). 유일한 문제는 두 번째가 RewriteCond더 이상 작동하지 않는다는 것입니다. 어쨌든 .htaccess파일 에 대한 상대 경로로 액세스된 경로를 동적으로 확인하는 방법이 있습니까 ?

예를 들어:

domain.com/prefix/파일 디렉토리 가 있는 사이트가 있는 경우 .htaccess.

NOT FORCED TO REWRITE -> domain.com/prefix/index.php
FORCED TO REWRITE -> domain.com/prefix/rewrite/index.php

domain.com/파일 디렉토리 가 있는 사이트가 있는 경우 .htaccess.

NOT FORCED TO REWRITE -> domain.com/index.php
FORCED TO REWRITE -> domain.com/rewrite/index.php

답변1

해결책이 있지만 간단하지는 않습니다.

사용 RewriteMap.

내가 할 방법은 다음과 같습니다.

만들기RewriteMap파일. 보다자세한 내용은 여기. 이것은 다음 내용이 포함된 매우 간단한 텍스트 파일입니다. 첫째, 잘못된 URL'/' 없이, 그 다음에한 공간(적어도) 다음과 같이 올바른 URL을 입력하세요.

ok​ /folder1/
ok /folder2/
ok /folder3/

그런 다음 코드는 다음과 같습니다.

RewriteMap myfoldermap \
  dbm:/web/htdocs/yourwebsite.com/rewriterules/myfoldermap.map

RewriteCond %{REQUEST_URI} ^/([^/]+)/(.*)
# Now in '%1' there's the name of the folder
# Don't touch the URL but...
# ...check if it exists in the map file
RewriteRule (.*) - [QSA,E=FOLDERMAP:${myfoldermap:%1|notfound}]

# if the environment is neither empty or not found...
RewriteCond %{E:FOLDERMAP} !^(notfound|)$
# this means we found something...
# => apply the RewriteRule
RewriteRule ^(.*)$ router.php [L,QSA]

작동하지 않으면 제가 자주 사용하는 "두 가지 힌트"를 읽고 질문에 재작성 로그를 추가하세요.

두 가지 힌트:

호스팅된 환경이 아닌 경우(= 자체 서버이고 파일뿐만 아니라 가상 호스트도 수정할 수 있는 경우 .htaccess) 다음 지시문을 사용해 보세요 RewriteLog. 이러한 문제를 추적하는 데 도움이 됩니다.

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

정규식을 확인하는 데 내가 가장 좋아하는 도구는 다음과 같습니다.

http://www.quanetic.com/Regex(preg(PCRE) 대신 ereg(POSIX)를 선택하는 것을 잊지 마세요!)

답변2

하다강제로 다시 작성요청한 파일이나 디렉터리가 존재하는 경우에도 다시 쓰기 규칙이 작동해야 한다는 의미입니까?

그렇다면 파일/디렉토리 생성을 피하십시오.

또는 두 가지 규칙으로 구분합니다.

RewriteRule ^/(always|rewrite|these|dirs)/ router.php [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ router.php [L,QSA]

답변3

내가 당신을 올바르게 이해하고 있다면 (특히 당신이 무엇을 의미하는지 모르겠습니다강제로 다시 작성),RewriteBase당신을위한 것입니다.

지금:같은 것을 사용하지 마십시오domain.com:-)

관련 정보