【问题标题】:htaccess trailing slash issuehtaccess 斜杠问题
【发布时间】:2015-11-26 06:16:23
【问题描述】:

我遇到了一些问题,因为没有尾部斜杠 url。我用谷歌搜索,但无法得到确切的结果。

  From Url : local.xxxx.com/stories

当我尝试使用上述网址时,它会重定向到

  To Url : local.xxxx.com/sapp/View//stories/

Htaccess:

DirectorySlash Off
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/app/View/%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/app/View/%{REQUEST_URI} -d
RewriteRule ^ /app/View/%{REQUEST_URI} [L]

现在我收到 403 Forbidden 错误。您无权访问此服务器上的 /app/View//stories。

如果我要添加斜杠,那么它可以完美地工作。如果没有斜杠,如果没有参数,我们可以在url末尾添加斜杠。

任何机构都可以建议我如何实现这一目标。

【问题讨论】:

  • 你在哪里添加斜杠?
  • 没关系... @anubhava 来救援。 ;)
  • 如果我们在 URL 的末尾添加斜杠,它会正常工作。如果 url 末尾没有斜杠,它会重定向到 views 文件夹

标签: php .htaccess trailing-slash


【解决方案1】:

这很可能是因为 /app/View/stories/ 是一个真实的目录,而 Apache 的 mod_dir 正在添加一个尾部斜杠。

您可以使用此代码进行修复:

DirectorySlash Off
RewriteEngine On

# internally add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/app/View/%{REQUEST_URI} -d
RewriteRule [^/]$ /app/View/%{REQUEST_URI}/ [L] 

RewriteCond %{DOCUMENT_ROOT}/app/View/%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/app/View/%{REQUEST_URI} -d
RewriteRule ^ /app/View/%{REQUEST_URI} [L]

【讨论】:

  • @Anuhava,仍然 url 重定向到 /app/View/stories/。你的假设是完美的。但它不适合我。
  • 问题已更新。如果没有参数和 .php 扩展名,我们可以在 url 的末尾添加斜杠。请检查一次
  • 在浏览器中输入http://domaon.com/app/View/stories会发生什么?那也给403吗?尝试在 .htaccess 顶部添加 Options +Indexes
  • @anubava。我通过使用下面的代码解决了这个问题。 RewriteCond %{DOCUMENT_ROOT}/app/View/%{REQUEST_URI} -d RewriteRule ^ /app/View/%{REQUEST_URI}/ [L]
  • 嗯,没错,很高兴它起作用了(答案已根据您的工作规则更新)
猜你喜欢
  • 2016-07-14
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
相关资源
最近更新 更多