如何制作一个干净的URL(htaccess URL重写)

时间:2017-02-07 15:14:41

标签: php apache .htaccess mod-rewrite url-rewriting

我使用支持系统,我想重写URL以使它们更加SEO友好和更好的可读性。我对.htaccess重写规则/正则表达式有非常基本的了解,我尝试了一百件事,但无法使其正常工作。

支持系统安装在此URL上:

http://www.example.com/help/

代码为KB中的每篇文章生成此URL:

http://www.example.com/help/index.php?/selfhelp/view-article/article-1

代码正在为KB中的类别概述生成此URL:

http://www.example.com/hulp/index.php?/selfhelp/categories

如何将这些URL重写为可读和SEO友好的内容,如:

http://www.example.com/help/article/article-1

对于类别:

http://www.example.com/hulp/categories

如果可能的话?如果是这样,我怎么能用.htaccess做到这一点。

1 个答案:

答案 0 :(得分:0)

您可以将友好网址设为这样,并重定向旧网址:

# Rewrite new URLs
RewriteRule ^help/article/(.+)$ help/index.php?/selfhelp/view-article/$1 [E=rewritten:1]
RewriteRule ^help/categories$ help/index.php?/selfhelp/categories [E=rewritten:1]

# Redirect old URLs
RewriteCond %{ENV:rewritten} !=1
RewriteCond %{ENV:REDIRECT_rewritten} !=1
RewriteCond %{QUERY_STRING} ^/selfhelp/view-article/(.+)$
RewriteRule ^help/index\.php$ /help/article/%1 [R=301,L]

RewriteCond %{ENV:rewritten} !=1
RewriteCond %{ENV:REDIRECT_rewritten} !=1
RewriteCond %{QUERY_STRING} =/selfhelp/categories
RewriteRule ^help/index\.php$ /help/categories [R=301,L]