【问题标题】:.htaccess code showing 500 internal error.htaccess 代码显示 500 内部错误
【发布时间】:2019-09-04 01:47:41
【问题描述】:

您好,我正在尝试制作博客演示,并且已经有了一些漂亮的 URL 代码。

我有一个网址www.xyz.com

还有一个搜索网址www.xyz.com/search/this+is+a+search+text

在搜索网址中,参数search 是页面名称,this+is+a+search+text 是我将要解析的参数

我已经在下面有一个 .htaccess 代码

# code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]

# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\d]+)$ app/index.php?page=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)$ app/post.php?category=$2 [L]

我在搜索页面使用下面的代码

# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php&searchstring=$2&page=$3 [L,QSA]

但在使用代码时,我收到 500 条内部错误消息,但我无法弄清楚错误是什么!

如果有人能帮助我解决这个逻辑,我将不胜感激。

【问题讨论】:

  • 请检查您的错误日志文件,会有更多关于 500 错误的信息。
  • 上面写着[Sun Apr 14 00:24:04.450503 2019] [core:error] [pid 7204:tid 1244] [client ::1:11856] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://www.myblog.com/diy
  • 您是否按照此日志消息的提示进行操作?设置您的LogLevel debug 可能会让您更接近关于内部重定向为何不断发生的答案。
  • 怎么做?
  • 您的规则相互冲突。最好把需求写清楚,以便在重构规则时获得更好的帮助。

标签: php .htaccess


【解决方案1】:

实现/category/slug和搜索页面的重写是一样的。搜索请求匹配first - 实现重写并运行app/post.php...

【讨论】:

  • 你能帮我更新一下代码吗,这样我就能明白我哪里错了。
【解决方案2】:

回答您对 JarekBaran 的后续问题

JarekBaran:实现/category/slug和搜索页面的重写是一样的。

这意味着RewriteCond 对于cateology/slug 页面和搜索页面的漂亮URL 代码是相同的。

这意味着将始终使用第一个匹配项,因此:

RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]

总会在之前触发

RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]

这样您将无法使用搜索功能。您可以考虑添加一个参数来识别是否进行了搜索。

【讨论】:

  • 没有必要在 htaccess 正则表达式中转义斜杠,因为 / 被视为横向字符而不是分隔符。
  • 那么你有什么建议,我应该如何更新代码以使其工作?我最初想将www.xyz.com/search/ 作为 url 参数,然后我将添加参数
【解决方案3】:

您的问题是您的规则以(.+) 开始,它将匹配任何一项或多项。这意味着您还匹配了/ 字符。因此,您的 2 个参数重定向中的一些将您的 url 与 3 个参数匹配。你最好从这样的东西开始 - ([a-zA-Z0-9-]+)

您的搜索重写应该是这样的 - RewriteRule ^/?([a-zA-Z0-9-]+)/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]

我在 this htaccess testing tool 上测试了该重写,它正在工作。

也就是说您的其他规则也需要更改,因为它们都以(.+) 开头。之后,您的类别重写将与您的搜索重写发生冲突。

但是,这可能对您有用:

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^/?category/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^/?category/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]

 # code to make pretty URLS for search page
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^/?search/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^/?([a-zA-Z0-9-]+)/([\d]+)$ app/index.php?page=$2 [L]

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^/?category/([\w-]+)$ app/post.php?category=$2 [L]

【讨论】:

  • 不工作,仍然是内部 500 错误,说明内部重定向过多
  • 如果你仍然得到它,那么你没有使用我放在那里的整个代码块。我刚试过。它做了重定向。我检查了重定向将创建的 url,它与任何规则都不匹配。
猜你喜欢
  • 2016-01-11
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多