重写本地主机和实时环境的规则 [英] Rewrite rules for localhost AND live environment

查看:29
本文介绍了重写本地主机和实时环境的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加可在本地环境(localhost)和我的liveserver上运行的重写器.

I want to add rewriterules that works in local environment (localhost) and on my liveserver.

为什么?

在本地测试项目并将其上传到实时服务器时,我不想更改规则.

I don't want to change rules when I test my project locally and upload it to the liveserver.

  1. 添加规则

这里有个例子

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

更改为

(ANY-URL)/somepage

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

So I used a rewritemod generator and pasted this into it:

index.php?page=somepage

我得到的重写器如下:(当然,我的.htacces以RewriteEngine On开头)

The rewriterule I got, looks like this: (of course my .htacces starts with RewriteEngine On)

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

当我尝试进入(http:) //localhost/myproject/development/index.php?page=login时,它会将我发送到本地开发环境的根目录.但是地址栏中的URL不变.

When I try to get to (http:) //localhost/myproject/development/index.php?page=login it sends me to the root directory of my local development envirment. But the URL in the adressline doesn't change.

测试

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

Of course I tried some other Rules by pasting the whole URL into the generator just to test if the rewrite thing works. Also here the URL doesn't change to short-url but the server cant find stylesheets and javascripts anymore. I got redirected to the index.php

可能的解决方案吗?

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

我的.htacces位于此处:

My .htacces is located here:

//localhost/myproject/development/.htaccess

稍后,我也想更改如下所示的路径:

Later I also want to change paths that look like this:

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

也是这里,我正在寻找一种适用于两种环境的解决方案.

Also here a I'm looking for a solution that works on both environments.

推荐答案

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

Since the htaccess is located inside a sub-directory, use RewriteBase:

RewriteEngine On
RewriteBase /myproject/development/

完成后,您将在php/html文件中使用base元素.这将解析页面中脚本/样式表的地址.

Once that is done, you use the base element in your php/html files. This will resolve the addresses for the scripts/stylesheets in your pages.

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

现在,htaccess中的重写将是:

Now, the rewrites in htaccess would be:

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

这篇关于重写本地主机和实时环境的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆