2017-10-04 4 views
0

처음으로 .htaccess URL 재 작성의 세계에 내 발가락을 담그고 첫 번째 장애물에 넘어 섰습니다. 그러나 확실한 뭔가를 놓치고 확신 해요 ... :)htaccess 문제를 사용하여 URL 재 작성

내 htaccess는 현재/wordpress에서 실행하지만 다시 작성되어 도메인의 최상위 수준에 나타나도록 다시 작성된 Wordpress입니다.

php_value short_open_tag 1 

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 

# add a trailing slash to /wp-admin 
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L] 

RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule^- [L] 
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) wordpress/$2 [L] 
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ wordpress/$2 [L] 
RewriteRule . index.php [L] 

</IfModule> 

나는 액세스하는 사람을 위해 재 작성 규칙을 추가 할 : xxx는 숫자 가 /간행물/퍼브-XXX/

생각입니다 /publication-details.php?id=xxx을 (이런 식으로 작동합니다 :

RewriteRule ^publication-details.php?id=([0-9]+) /publications/pub-$1 [NC] 

하지만 기존 규칙에서 어디서 주문 했든 상관 없지만 작동하지 않습니다. /publication-details.php를 방문하면 404 (Apache 기본값)가 표시됩니다. URL을 확장하지 않으려 고 시도하면// Wordpress 404가 표시됩니다.

무엇이 없습니까?

미리 감사드립니다.

답변

0

한다 RewriteRule 만 URI가 아닌 쿼리 문자열에서 작동합니다.

당신이하려고하는 일을하기 위해서는 쿼리 문자열을 확인하기 위해 RewriteCond (= Condition)가 필요합니다.

RewriteCond %{QUERY_STRING} id=([0-9]+) 
RewriteRule ^publication-details.php /publications/pub-%1? [R=302,L] 

RewriteCond는 다음 RewriteRule의 조건입니다. 조건 ($ 1은 규칙에서 일치하는 항목에만 해당)에서 일치 항목을 사용하는 규칙은 $ 1이 아니라 % 1입니다. 마지막에 작은 따옴표를 추가하여 쿼리 문자열의 추가를 비활성화했습니다. 그렇지 않으면/publications/pub-12345? id = 12345로 리디렉션됩니다.

1

을 편집 퍼머 %의 postname %

add_filter('rewrite_rules_array','my_insert_rewrite_rules'); 
add_action('wp_loaded','my_flush_rules'); 

function my_insert_rewrite_rules($rules) 
{ 
     $newrules = array(); 
     $newrules['([^/]+)/publication/pub-([^/])/?$'] = add_rewrite_rule('([^/]+)/publication/pub-([^/])?$', 'index.php?pagename=$matches[1]&pub-id=$matches[3]', 'top'); 
     return $newrules + $rules; 
}   

add_action('wp_loaded','my_flush_rules'); 

// flush_rules() if our rules are not yet included 
function my_flush_rules(){ 
    $rules = get_option('rewrite_rules'); 
    if (! isset($rules['([^/]+)/publication/pub-([^/])/?$'])) { 
      global $wp_rewrite; 
      $wp_rewrite->flush_rules(); 
    } 
} 
+0

감사합니다. htacces에 직접 규칙을 삽입하는 대신 이점이 있습니까? – user3204476