【问题标题】:URL rewriting apache2URL重写apache2
【发布时间】:2013-07-15 20:55:10
【问题描述】:

怎样才能改写

anyhost.com/argument1/parameter2/some-text/

作为

anyhost.com/index.php?path=argument1/parameter2/some-text/

anyhost.com/index.php?page=argument1&subpage=parameter2&subsubpage=some-text

或类似的?

【问题讨论】:

  • 你试过什么?另外,出于 SEO 等的目的,我认为您可能会向后看。

标签: mod-rewrite url-rewriting apache2 regex


【解决方案1】:

应该这样做:

RewriteEngine On

# First example
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]

# Second example
RewriteRule ^(.*)/(.*)/(.*)$ index.php?page=$1&subpage=$2&subsubpage=$3 [L,QSA]

QSA flag 用于将原始 URL 中的任何查询字符串自动附加到发送到 index.php 的查询字符串中;如果您不需要该功能,可以将其删除。

【讨论】:

  • 是的,我的第一条规则正是我想要的。非常感谢!
【解决方案2】:
RewriteRule (.*)?/ index.php?path=$1

这是第一个版本。

RewriteRule (.*)/(.*)/(.*)?/ index.php?page=$1&subpage=$2&subsubpage=$3

这是第二个。您始终可以使用 this tool 测试您的重写。

【讨论】:

    猜你喜欢
    • 2010-11-11
    • 1970-01-01
    • 2011-08-08
    • 2021-11-14
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2014-08-10
    • 2018-02-26
    相关资源
    最近更新 更多