【问题标题】:how to modify .htaccess file to give QUERY_STRING?如何修改 .htaccess 文件以提供 QUERY_STRING?
【发布时间】:2018-01-29 03:01:37
【问题描述】:

我的网站中有以下 htaccess 文件,它在过去两年中运行良好。最近我将服务器迁移到一个新的托管合作伙伴,似乎它没有按预期工作。我已经确认服务器支持 mod_rewrite 模块。我可以看到 [QUERY_STRING] => 是 null 我指定的任何 URL,并且所有 URL 都路由到主页。谁能告诉我需要修改什么。我在stackoverflow中看到了很多答案,但对我没有任何帮助。我只是这个 htaccess 文件的初学者。

Options +FollowSymLinks
RewriteEngine On
Options -Indexes

# REWRITING index.php AS index #
RewriteRule   ^index/?$   index.php  [NC]

# Route The Pages #
RewriteRule   ^index/([{a-z}]+)/?$   index.php?$1  [NC,L]

RewriteRule   ^index/home/([0-9]+)/?$   index.php?home&p_id=$1  [NC,L]
RewriteRule   ^index/page/([{a-z}{\-\}{0-9}]+)/?$   index.php?page&show=$1  [NC,L]

RewriteRule   ^index/gallery/([{image}{video}]+)/?$   index.php?gallery&type=$1  [NC,L]
RewriteRule   ^index/gallery/([{image}{video}]+)/([0-9]+)/?$   index.php?gallery&type=$1&album=$2  [NC,L]

【问题讨论】:

  • 为什么你的模式中有花括号?您能告诉我一些无效的网址吗?
  • 其中一个网址是 tgmvidyaniketan.edu.in/index/gallery/image

标签: php .htaccess mod-rewrite


【解决方案1】:

如果您匹配videoimage,则没有理由将{video} 设置为{ 和}will match literally{video}`。

让您的 .htaccess 以这种方式访问​​:

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On

# REWRITING index.php AS index #
RewriteRule ^index/?$ index.php [NC,L]

# Route The Pages #
RewriteRule ^index/([a-z]+)/?$ index.php?$1 [NC,L,QSA]

RewriteRule ^index/home/([0-9]+)/?$ index.php?home&p_id=$1 [NC,L,QSA]

RewriteRule ^index/page/([a-z0-9-]+)/?$ index.php?page&show=$1 [NC,L,QSA]

RewriteRule ^index/gallery/(image|video)/?$ index.php?gallery&type=$1 [NC,L,QSA]

RewriteRule ^index/gallery/(image|video)/([0-9]+)/?$ index.php?gallery&type=$1&album=$2 [NC,L,QSA]

【讨论】:

  • 我正要发布完全相同的版本 - @Ricky 你可以在这里看到一个演示:htaccess.mwl.be?share=680305d4-dc3c-5d94-8016-1f890e9b0220
  • 我已经测试过它会产生 500 错误。我的 Apache 版本是 2.2.17
  • 成功了!!!!非常感谢兄弟:)你能告诉我有什么区别
  • 选项MultiViews(参见httpd.apache.org/docs/2.4/content-negotiation.html)由运行之前 mod_rewriteApache's content negotiation module使用,并使Apache服务器匹配文件的扩展名。因此,如果 /index 是 URL,那么 Apache 将提供 /index.php
  • 再次感谢 :)
【解决方案2】:
Options +FollowSymLinks
RewriteEngine On
Options -Indexes

空查询字符串与启用MultiViews 一致(可能是服务器更新的结果)。尝试在 .htaccess 文件的顶部禁用 MultiViews

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On

如果启用了MultiViews,那么对/index/<something> 的请求将导致对/index.php/<something> 的内部子请求,并且您剩余的指令都不会匹配。


但是,您仍然需要将您的正则表达式更新为 anubhava 建议的内容,因为您当前的正则表达式可能比您预期的要匹配得多。但是您当前的模式是模棱两可的。例如,[{a-z}{\-\}{0-9}]+ 应该匹配什么?看起来您可能打算将其设为<letter>-<digit>?但是,它目前匹配字母、数字和连字符的任意组合(这是 anubhava 的解释方式)?

【讨论】:

  • 谢谢怀特先生。选项 +FollowSymLinks -MultiViews 完成了工作
  • 不客气。 MultiViews(mod_negotiation 的一部分)是与 mod_rewrite 指令冲突的常见原因(当引用没有文件扩展名的文件时)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-07
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 2017-12-04
  • 2011-05-08
相关资源
最近更新 更多