htaccess 用于本地测试和实时服务器

htaccess for local testing and live server

我已经接近我认为的最终解决方案了。 .htaccess 看起来像这样:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /myproject/development/
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page= [L]
RewriteRule ^((?!index\.php)[^/]+)/([A-Za-z0-9]+)/([0-9]+)/([0-9]+)/?$ index.php?page=&keyword=&zip=&range= [L,R]

我不需要第一个规则的 RewriteBase(对此有点惊讶)但是如果我添加第二个规则并打开这个 URL:

//localhost/myproject/development/somepage/test/13245/50

否则页面将被打开,但当然没有样式表和 javaScripts 可以找到。

1.) 目标: 我想在不更改或添加 rewriteBase 的情况下使用第二个 rewriteRule。我需要在 .htaccess 中更改什么,以便我可以在没有 rewriteBase 的情况下继续测试我的项目。

为什么:正如我之前所问的,我想在本地和实时服务器上测试我的项目,而不需要对项目配置进行太多更改。

2.) [R] 标志 如果我请求

//localhost/myproject/development/somepage/test/53229/2000

当然在地址行中我们有这个 URL

//localhost/myproject/development/index.php?page=somepage&keyword=test&zip=12345&range=2000

为了避免这种行为,我应该删除 R-Flag。但是 CSS 和 JS 再也找不到了。另外在这里,我正在寻找一个没有 rewritebase、basepath、虚拟主机等的解决方案,如果可能的话。

这是我开始的地方:

从外观上看,您不希望 R 标志出现在第 2 个 RewriteRule 上。这打败了你的 "pretty" URLs.

的对象

But then the CSS and JS can't be found anymore.

因为您使用的是 CSS 和 JS 文件的相对路径。将您的路径更改为 root-relative(以斜杠开头),或使用页面 head 部分中的 base 元素,以指示 URL 所有相对 URLs 是相对于:

<base href="http://www.example.com/page.html">

更多信息:
https://developer.mozilla.org/en/docs/Web/HTML/Element/base


I don't need the RewriteBase for the 1st rule(was a little surprised about that)

第一条规则(内部重写)不需要 RewriteBase,因为目录前缀(导致此 .htaccess 的文件系统路径)文件)自动添加回相对路径替换。

然而,对于外部重定向(即。R标志),目录前缀没有意义,所以你需要指定一个相对根目录(以斜杠开头)或绝对 URL。或者指定适当的 RewriteBase 指令,它会覆盖将为相对替换添加的 URL-path(仅此而已)。