2017-10-04 131 views
0

只是將我的腳趾首次插入到.htaccess URL重寫的世界中,並在第一個障礙中墮落。不過我相信我缺少明顯的東西... :)使用htaccess問題重寫網址

我的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> 

我想添加一個重寫規則採取任何人訪問: /publication-details.php?id=xxx(其中xxx是一個數字 到 /出版/酒館-XXX/

我想像這樣的東西會工作:

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

但我試過,作爲一個規則,但它不工作 - 無論我在現有的規則中訂購它。試圖訪問/publication-details.php只是獲得一個404(Apache默認)。如果我嘗試/什麼(即沒有擴展名的網址),我得到的Wordpress 404。

我錯過了什麼?!

在此先感謝。

回答

0

重寫規則只適用於該URI,而不是查詢字符串。

要做你想做的事情,你需要一個RewriteCond(= Condition)來檢查查詢字符串,例如:

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

RewriteCond是以下RewriteRule的條件。請注意,它是Rule中的%1,而不是$ 1,用於使用Condition中的匹配($ 1僅用於Rule中的匹配)。我在最後添加了一個單引號來禁止添加查詢字符串,否則它會重定向到/ 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