2014-12-24 78 views
1

我想知道如何使用htaccess將動態URL匹配到靜態URL。我的代碼如下: -如何使用htaccess將動態URL與靜態URL匹配?

我的PHP函數: -

public static function ToTagLinks($tags, $page = 1){ 
    $tags = explode(',', $tags); 
    foreach ($tags as &$tag) { 
//$link ='tagged/'.self::CleanUrlText($tag); 
     $link ='index.php?TagSearchResults&TagName='.self::CleanUrlText($tag) ; 
     if($page > 1) 
     $link .='&Page='.$page; 
     $tag = '<a class="taglist" href="'.self::Build($link). 
          '" title="show post tagged ' . $tag . '">' . $tag . '</a>'; 
    } 
    return implode(', ', $tags);    
} 

我htaccess文件:

<IfModule mod_rewrite.c> 
# Enable mod_rewrite 
RewriteEngine On 

# Specify the folder in which the application resides. 
# Use/if the application is in the root. 
RewriteBase /rswebtek_blog 

# Rewrite to correct domain to avoid canonicalization problems 
# RewriteCond %{HTTP_HOST} !^www\.example\.com 
# RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 

# Rewrite URLs ending in /index.php or /index.html to/
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html?)\ HTTP 
RewriteRule ^(.*)index\.(php|html?)$ $1 [R=301,L] 

# Rewrite category pages 
RewriteRule ^.*-c([0-9]+)/page-([0-9]+)/?$ index.php?CategoryId=$1&Page=$2 [L] 
RewriteRule ^.*-c([0-9]+)/?$ index.php?CategoryId=$1 [L] 

#Redirect search results 
RewriteRule ^search-results/find-(.*)/page-([0-9]+)/?$ index.php?SearchResults&SearchString=$1&Page=$2[L] 
RewriteRule ^search-results/find-?(.*)//?$ index.php?SearchResults&SearchString=$1&Page=1 [L] 

#Redirect tag search results 
RewriteRule ^tagged/(.*)/page-([0-9]+)/?$ index.php?TagSearchResults&TagName=$1&Page=$2[L] 
RewriteRule ^tagged/?(.*)//?$ index.php?TagSearchResults&TagName=$1&Page=1 [L] 

# Rewrite subpages of the home page 
RewriteRule ^page-([0-9]+)/?$ index.php?Page=$1 [L] 

# Rewrite Post details pages 
RewriteRule ^.*-p([0-9]+)/?$ index.php?PostId=$1 [L] 
</IfModule> 
# Set the default 500 page for Apache errors 
ErrorDocument 500 /rswebtek_blog/500.php 

#Set the defualt 404 error page 
ErrorDocument 404 /rswebtek_blog/404.php 

我等各個環節正常工作,但這個代碼標籤搜索鏈接無法正常工作404 .php錯誤發生,但如果我使用動態網址,它的工作很好。

+0

Anubhava姬在我上面的鏈接功能,我想補充一點,這種類型的鏈接wwww.example.com/tagged/php的,但我的動態網址是的index.php? TagSearchResults&TagName = php –

+0

Anubhava我編輯了我的上面的代碼 –

回答

1

嘗試那些2個規則:

#Redirect tag search results 
RewriteRule ^tagged/([^/]+)/page-(\d+)/?$ index.php?TagSearchResults&TagName=$1&Page=$2 [L,QSA] 
RewriteRule ^tagged/([^/]+)/?$ index.php?TagSearchResults&TagName=$1&Page=1 [L,QSA] 
+0

非常感謝代碼工作良好。請問我的代碼中有什麼問題? –

+0

這是一個正則表達式問題。您在正則表達式中使用了'。*'和'//'。 – anubhava