【问题标题】:apache mod_rewrite user URLs to index.phpapache mod_rewrite 用户 URL 到 index.php
【发布时间】:2014-12-30 18:45:42
【问题描述】:

我一直有一个相当标准的 apache 配置。现在我们正在引入一个新概念,即用户会话特定的 URL,它将改变一些事情。基本上我们有一个 DocumentRoot 和任何东西,例如:

http://example.com/ 会在DocumentRoot 指令中命中 index.html。

但现在我希望能够做类似的事情

http://example.com/uid/5/ http://example.com/uid/2

那些仍然应该在已设置的 DocumentRoot 中点击 index.html。 URL 主要用于服务器端和客户端脚本,以便能够执行自己的任务。

在 Apache 中处理此问题的最佳方法是什么?这里还需要 mod_rewrite 吗?

我还需要能够支持现有路径,例如:

http://example.com/foo/bar/something.php 将被重写为 http://example.com/uid/3/foo/bar/something.php,但仍会在文件系统上与以前一样访问相同的位置。

【问题讨论】:

  • 你可能确实需要 mod_rewrite。 /uid/3/foo/bar/something.php 是否真的作为包含所有真实目录的完整路径存在?
  • foo/bar/something.php 存在于 DocumentRoot 下的文件系统中。 /uid/3 是我全面添加的“虚拟路径”。注意:foo/bar/something.php 默认已经可以使用了。

标签: apache mod-rewrite


【解决方案1】:

您可以通过将此代码放入您的 htaccess 中来使用 mod_rewrite

RewriteEngine On
RewriteRule ^uid/([1-9][0-9]*)/(.+)$ /$2?uid=$1 [L]

示例:
http://example.com/foo/bar/something.php -> 不变
http://example.com/uid/3/foo/bar/something.php -> 重写为/foo/bar/something.php?uid=3


编辑:不附加uid

RewriteEngine On
RewriteRule ^uid/[1-9][0-9]*/(.+)$ /$1 [L]

【讨论】:

  • 如果我不想将uid=3 附加为查询字符串怎么办?我想保留 URL 原样,但请确保它在文件系统上找到正确的路径。
  • 我做了一个例子,如果你不想要它你可以摆脱它(见我编辑的答案)
猜你喜欢
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 2014-07-04
相关资源
最近更新 更多