重写本地主机和实时环境的规则

Rewrite rules for localhost AND live environment

我想添加在本地环境 (localhost) 和我的实时服务器上工作的重写规则。

为什么?

我不想在本地测试我的项目并将其上传到liveserver时更改规则。

  1. 添加规则

    举个例子

    (ANY-URL)index.php?page=somepage
    

    改为

    (ANY-URL)/somepage
    

    所以我使用了 rewritemod 生成器并将其粘贴到其中:

    index.php?page=somepage
    

    我得到的重写规则看起来像这样:(当然我的 .htacces 以 RewriteEngine On 开头)

    RewriteRule ^([^/]*)$ /?page= [L]
    

    当我尝试到达 (http:) //localhost/myproject/development/index.php?page=login 时,它会将我转到本地开发环境的根目录。但是地址行中的URL没有改变。

  2. 测试中

    当然,我尝试了一些其他规则,将整个 URL 粘贴到生成器中,只是为了测试重写是否有效。 同样在这里 URL 不会更改为 short-url 但服务器无法再找到样式表和 javascript。我被重定向到 index.php

  3. 可能的解决方案?

    • 也许跟那个有关"RewriteBase"?
    • 我必须设置基本路径吗?

我的 .htacces 位于此处:

//localhost/myproject/development/.htaccess

以后我也想改成这样的路径:

(ANY-URL)index.php?page=somepage&second=hello&third=world&fourth=cool

我也在寻找适用于两种环境的解决方案。

由于 htaccess 位于子目录中,使用 RewriteBase:

RewriteEngine On
RewriteBase /myproject/development/

完成后,您可以在 php/html 文件中使用 base 元素。这将解析您网页中 scripts/stylesheets 的地址。

<base href="/myproject/development/" />

现在,htaccess 中的重写将是:

RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page= [L]