【问题标题】:Mod-Rewrite: Need help to optimize the Rewrite RulesMod-Rewrite:需要帮助来优化重写规则
【发布时间】:2012-10-23 23:26:41
【问题描述】:

在我的 .htaccess 链接中更改某些内容之前如下所示:

现在我用这段代码创建了一个 .htaccess:

RewriteEngine on
RewriteRule ^index.php / [R,L,QSA]

现在上面的链接在浏览器地址栏中是这样的:

这几乎就是我想要的,但我怎样才能让它看起来像这样呢?

我尝试了很多不同的东西,但 mod_rewriting 还不是我的强度。 我还希望像 Google 这样的搜索引擎使用新的 URL (SEO)。

【问题讨论】:

  • 我将解决方案发布为答案。

标签: mod-rewrite url-rewriting


【解决方案1】:

以下是将链接从http://www.domain.tld/index.php?page=blog 更改为http://www.domain.tld/blog 的解决方案是:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\w+$ index.php?page=$0 [L]
RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{QUERY_STRING} ^page=(\w+)$
RewriteRule ^index\.php$ /%1? [R=301,L]

对于像这样的链接:http://www.domain.tld/index.php?page=single_news&id=1&headline=This%20Is%20A%Headline

解决办法是:

RewriteRule ^blog/(\d+)-([\w-]+)$ index.php?page=single_news&id=$1&headline=$2

使用此代码后,链接如下所示:http://www.domain.tld/blog/2-this-is-a-headline

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2015-03-23
    • 2013-02-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多