【问题标题】:Using index.php to manage all url paths in my site使用 index.php 管理我网站中的所有 url 路径
【发布时间】:2011-10-31 01:07:57
【问题描述】:

我一定遗漏了一些明显的东西,因为这似乎不应该是一个难题。我希望通过我的 index.php 处理我网站上的所有 url 路径(http://example.com、http://example.com/admin、http://example.com/happy/happy/joy/joy 等)。我以前见过这样做,但是我不知道该怎么做。

【问题讨论】:

  • 如果您使用的是 Apache,您的 .htaccess 文件中使用 mod_rewrite 的规则就可以了。
  • 同意 Borealid,如果没有 Web 服务器的帮助,您将无法做到这一点。取决于服务器的风格将取决于你如何去做。例如,请参阅Zend Framework web server configuration

标签: php url path


【解决方案1】:

只需在你的根目录中创建一个.htaccess 文件:

# Turn on URL rewriting
RewriteEngine On

# Base directory
RewriteBase /

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

Apache 标志解释:

[L] - “Last”的别名,指示服务器重写规则已经结束,是时候在不更改浏览器 uri 的情况下执行内部重定向了。

[PT] - “Pass Through”的别名,允许根据 php.ini 模块的包含顺序将 Mod_Rewrite 操纵的 uri 传递给下一个类型的处理程序。有用取决于主题。

【讨论】:

  • Drupal 似乎使用RewriteRule ^ index.php [L]。有什么区别?
  • 查看更新后的帖子。在您的情况下不需要 L 标志,因为您想要的是重写浏览器的 uri。
  • 处理程序?这是否意味着另一个 RewriteRule 反过来?
  • 不,PT 将强制将 uri 作为参数/处理程序传递给下一个模块。
  • 所以没有它,其他模块无法获取更改后的 URI..?这是在配置文件中进行的一种命令式编程吗?
猜你喜欢
  • 2017-02-03
  • 2013-06-12
  • 2022-08-11
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 2010-10-28
  • 2010-09-29
相关资源
最近更新 更多