2011-12-22 4 views
0

내 코드 :이 작동다시 쓰기는 디렉토리에서 작동합니다. 그러나 파일이 아닌

RewriteEngine on 
RewriteBase/
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L] 
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L] 

. 하지만 파일 이름이나 디렉토리를 제공하면 실패합니다.

내가 원하는 무엇 : 사용자가 디렉토리 나 파일 이름을 요청하면

  • 는 홈 페이지로 리디렉션합니다.
  • 다른,이 규칙은 평가되어야한다 :

    RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]

내 사이트의 루트에있는 하나 개의 index.php 파일이 있습니다 : "http://mysite.com/index. PHP는 "

샘플 요청 :

User typed url --- Physical matched url --- Comments 
---------------------------------------------------------- 

http://mysite.com/cinema --- http://mysite.com/index.php?page=cinema --- display  cinema page 
http://mysite.com/contacus --- http://mysite.com/index.php?page=contactus ---  display contact us page 
http://mysite.com/article/iphone --- http://mysite.com/index.php?page=article&name=iphone --- display article 
http://mysite.com/images --- http://mysite.com/index.php --- redirect to index page, because it is a physical directory 
http://mysite.com/js --- http://mysite.com/index.php --- redirect to index page, because it is a physical directory 

내 이상적 상대 동맹 코드 :

RewriteEngine on 
RewriteBase/

RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.*)/$/[R,L] 

RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L] 
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L] 

위 코드를 사용하면 부분적으로 작동합니다. 즉

http://mysite.com/images --- http://mysite.com/?page=images is displayed in address bar 
http://mysite.com/images/ --- http://mysite.com/ is displayed in address bar 

내가 두 번째 조건 (첫 번째 조건의 라인 후)로 RewriteCond %{REQUEST_FILENAME} -f를 사용

, 그것은 작동하지 않습니다이다.

파일이나 디렉토리가 이 존재하지 않는 경우 나

답변

0

은 확실히 당신의 재 작성 규칙은 적용되어야 도와주세요?

RewriteCond

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
+0

감사하지만 그것은 효과가 없었습니다. 나는이 간단한 문제 때문에 정신적으로 우울한 상태입니다. 내 모든 스크립트는 문제없이 작동하지만이 URL 재 작성 만 문제가됩니다. 나는 해결책을 찾기 위해 몇 시간을 소비 해왔다. :( –

0

내 로컬 호스트에 그것을 시도하여 이러한 라인을 사용해보십시오,이 제대로 작동 :

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(.*)$/[R=301,L] 

RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L] 
RewriteRule ^article/([^/\.]+)/?$ /index.php?page=article&name=$1 [L] 

# For Future Reference, this works well for the final RewriteRule 
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?page=$1&name=$2 [L] 

를 대체 솔루션으로서, 당신은 또한 색인에 모든 URL을 다시 쓸 수있다 .php :

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(.*)$/[R=301,L] 

RewriteRule ^(.*)$/[L] 

첫 번째 RewriteRule이 실제로 리디렉션됩니다. 브라우저,하지만 두 번째는 그냥 번역 할 것입니다. 이것에 대한 단점은 index.php가 원래 URL의 REQUEST_URI를 받게된다는 것입니다 :

$parts = explode('/', $_SERVER['REQUEST_URI']); // array(0 => 'article', 1 => 'iphone', 2 => '') 
관련 문제