apache - mod_rewrite 和相对 url

标签 apache .htaccess mod-rewrite

我正在使用 mod_rewrite 和 .htacces 文件设置一些简单的 url 重写规则,但我遇到了一些问题。 如果我这样设置 .htacces:

Options +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteRule /index.html /index.php [L]

当我从浏览器调用这个 url 时: http://localhost/~dave/mySite/index.html 我收到 404 错误。

改用这个 .htacces

Options +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteRule /index.html http://localhost/~dave/mySite/index.php [L]

一切正常,我得到了我期待的 index.php 页面。 我是否被迫使用绝对 url 作为重写的目标?这是 Apache 配置问题吗?

我正在使用 Max OS X 10.6.2 及其标准 Apache 安装。

最佳答案

  1. RewriteBase 基本指令正在设置基本路径;您不需要在重定向规则中提供绝对路径。

  2. 规则模式不是纯文本;这是一个正则表达式。

  3. 如果您将完整的 URL 设置为目标,您会指示浏览器重定向到该其他位置;这种重定向对用户来说不是透明的。

总结一下,试试这个:

RewriteRule ^index\.html$ index.php [L]

关于apache - mod_rewrite 和相对 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2401022/

相关文章:

xcode - 检查进程是否正在从 cocoa 应用程序中运行

python - WSGI/Django : pass username back to Apache for access log

apache - 为什么端口更改在 hadoop 2.6.0 的伪分布式模式下不起作用?

php - 将 http 重定向到 https,但有异常(exception)

php - 从传入的 url 在 php 或 htaccess 中重定向

.htaccess - 如何向已使用 mod_rewrite 重写的 URL 添加其他 URL 变量

apache - 301 将非 www 重定向到 www

php - 在 PHP5 ubuntu 中自动加载扩展

javascript - 如何删除url页面上的#id

php - .htaccess 与产品(字符串)