【问题标题】:Correct syntax for mod_rewrite?mod_rewrite 的正确语法?
【发布时间】:2016-12-28 20:13:11
【问题描述】:

在 Apache 2.4.7 .htaccess 文件中使用的正确语法是什么

http://www.example.com/go/stuff

改写为:

http://www.example.com/index.php?tab=go&page=stuff?

我在RewriteCondRewriteRule 指令中使用什么语法?

【问题讨论】:

  • 抱歉,这不起作用。当我尝试这些时,我只会收到 500 或 404 错误,包括链接中的建议。
  • 您正在将这些变量 tab=go&page=stuff 传递给我假设的 index.php。
  • @Warwick:您能否展示您尝试的规则并说明导致 500 的 URL 是什么?
  • @Abhishekgurjar,对不起,是的,我正在传递给 index.php - 已经编辑了我的原始帖子。

标签: apache .htaccess mod-rewrite apache2.4


【解决方案1】:

您可以在站点根目录 .htaccess 中尝试此规则:

RewriteEngine On

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?tab=$1&page=$2 [L,QSA]

【讨论】:

  • 这是正确的,除了page=$2,而不是$1。这也有助于我了解我哪里出错了。我现在明白了!谢谢!
  • 啊,你是对的,这是一个错字,应该是page=$2
猜你喜欢
  • 1970-01-01
  • 2020-06-11
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
  • 2017-06-22
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多