【问题标题】:.htaccess rewrite rules for multiple parameters.htaccess 多个参数的重写规则
【发布时间】:2015-07-30 04:23:37
【问题描述】:

重写以下3个网址

http://example.com/index.php?page=searchhttp://example.com/search

http://example.com/index.php?page=profile&value=mynamehttp://example.com/myname

http://example.com/index.php?page=stats&type=dailyhttp://example.com/stats/daily

当前的 .htaccess 是这样写的:

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&value=([^\s&]+) [NC]
RewriteRule ^ index/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&type=([^\s&]+) [NC]
RewriteRule ^ index/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ index/%1/? [R=302,L,NE]

RewriteRule ^index/([^/]+)/([^/]+)/?$ /index.php?page=$1&value=$2 [NC,L,QSA]
RewriteRule ^index/([^/]+)/([^/]+)/?$ /index.php?page=$1&type=$2 [NC,L,QSA]
RewriteRule ^index/([^/]+)/?$ /index.php?page=$1 [NC,L,QSA]

我需要从第一个和第三个 URL 中删除 /index/,并从第二个 URL 中删除 /index/profile/。

欢迎所有的 cmets 和建议。

【问题讨论】:

  • 您当前的 .htaccess 规则存在问题,因为您将 page=X&value=Ypage=X&type=Y 都重定向到同一个 URL,即。 /index/X/Y - 这意味着你不能重写这个。您的两个RewriteRules 具有相同的模式,因此第二个永远不会匹配。它总是会重写回page=X&value=Ytype 丢失)。

标签: php .htaccess url-rewriting rewrite url-routing


【解决方案1】:

您不能让搜索和配置文件都是相同的正则表达式模式。例如,如果有人要求:

http://example.com/index/foo

他们是想访问名为“foo”的页面还是访问名为“foo”的用户的个人资料?您需要添加一些将两者分开的内容,例如:

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /index\.php\?page=profile&value=([^\s&]+) [NC]
RewriteRule ^ profile/%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&type=([^\s&]+) [NC]
RewriteRule ^ page/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)(\ |$) [NC]
RewriteRule ^ page/%1? [R=302,L,NE]

RewriteRule ^profile/([^/]+)/?$ /index.php?page=profile&value=$1 [NC,L,QSA]
RewriteRule ^page/([^/]+)/([^/]+)/?$ /index.php?page=$1&type=$2 [NC,L,QSA]
RewriteRule ^page/([^/]+)/?$ /index.php?page=$1 [NC,L,QSA]

这使您的网址看起来像:

http://example.com/profile/myname

http://example.com/page/search

http://example.com/page/stats/daily

【讨论】:

【解决方案2】:

大概这些是当前规则的 3 个例外(特殊情况),因此现有规则不应更改。 (?)

在您的RewriteBase 指令之后、现有规则之前添加以下“特殊情况”:

RewriteCond %{THE_REQUEST} /index\.php\?page=(search)
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{THE_REQUEST} /index\.php\?page=profile&value=(myname)
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{THE_REQUEST} /index\.php\?page=(stats)&type=(daily)
RewriteRule ^ /%1/%2? [R=302,L]

如果您随后需要在内部重写这些特定 URL 回“真实”参数化版本,那么在 .htaccess 文件的末尾:

RewriteRule ^(search)$ /index.php?page=$1 [L,QSA]
RewriteRule ^(myname)$ /index.php?page=profile&value=$1 [L,QSA]
RewriteRule ^(stats)/(daily)$ /index.php?page=$1&type=$2 [L,QSA]

(我已经删除了 NC 标志 - 如果你真的需要,可以添加回来。)

【讨论】:

  • 感谢您的反馈。我正在尝试从 url 中删除第一个查询并重写如下:example.com/profile/mynameexample.com/myname example.com/page/searchexample.com/page/search example.com/page/stats/dailyexample.com/stats/daily
  • 是的,这就是我认为上述指令应该实现的目标,对于您问题中所述的 3 个特定 URL(尽管您在上面的评论中陈述的 URL 不同?)。但是,如果这旨在成为“通用”重写,例如。 example.com/profile/<anyname> 那么这将无法按预期工作,因为它引入了歧义(无法转换回来的一对多关系)。
  • 但是上面建议的重写规则似乎不起作用。应用那些建议的重写规则时,最初列出的 url 不会打开。
  • “似乎不起作用”是什么意思?它会做任何事情吗?这些指令应该在给定的上下文中工作(我还在我自己的服务器上测试了它们以确保)。这些指令的顺序很重要(如上所述)。您的 .htaccess 中的其他指令可能会发生冲突吗?除了您的问题中所述的指令之外,您的 .htaccess 文件中还有其他内容吗?
  • 好的。尝试清除现有规则并仅使用您的规则 - 它确实有效!
猜你喜欢
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
  • 2017-11-21
  • 2013-06-29
  • 1970-01-01
  • 2013-02-09
相关资源
最近更新 更多