【问题标题】:How to Match Dynamic Url with Static Url using htaccess?如何使用 htaccess 将动态 URL 与静态 URL 匹配?
【发布时间】:2014-12-24 15:37:01
【问题描述】:

我想知道如何使用 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 错误发生但如果我使用动态 url 它工作良好。

【问题讨论】:

  • Anubhava Ji 在我上面的链接功能中我只想添加这种类型的链接 wwww.example.com/tagged/php 但我的动态网址是 index.php?TagSearchResults&TagName=php
  • Anubhava 我已经编辑了我上面的代码

标签: php .htaccess


【解决方案1】:

试试这两条规则:

#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]

【讨论】:

  • 非常感谢代码运行良好。请问我的代码有什么问题?
  • 这是一个正则表达式问题。您在正则表达式模式中使用了 .* 和 //。
猜你喜欢
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 2012-04-09
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多