在开发和实时服务器之间切换时不一致? [英] Inconsistency when switching between development to live server?

查看:22
本文介绍了在开发和实时服务器之间切换时不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 .htaccess 文件.特别是文件的相对路径.修复起来并不难,但很麻烦.

I always have trouble when moving a site that I've developed in localhost into my hosting server with the .htaccess file. Specifically with relative paths to the files. It's not hard to fix, but it's quite bothering.

例如:

  • ErrorDocument 404/foo/404.php 在托管,为了在本地主机上工作,我必须指定 ErrorDocument404/projectroot/foo/404.php.

  • Whereas ErrorDocument 404 /foo/404.php works in the hosting, for it to work in localhost I have to specify ErrorDocument 404 /projectroot/foo/404.php.

使用 mod_rewrite, RewriteRule ^example/$example.php [L,NC]在本地主机上工作,但要在主机中工作,我必须使用RewriteRule ^example/$/example.php [L,NC](注意文件名前的斜线)

When using mod_rewrite, RewriteRule ^example/$ example.php [L,NC] works in localhost, but for it to work in the hosting I must use RewriteRule ^example/$ /example.php [L,NC] (note the slash before the file name).

可能是因为我在 Windows 上运行 localhost 而我的主机在 Linux 上,但我认为这不是问题.

It could be because I'm running localhost on Windows and my hosting is on Linux, but I don't think that's the problem.

所以我的问题是,在本地或远程服务器上工作时,如何确保文件路径正确?.htaccess 文件的工作目录"是什么?

So my question is, how can I ensure the paths to the files are correct when working locally or on the remote server? What is the "working directory" for the .htaccess file?

推荐答案

这看起来可能比看起来稍微复杂一些,但我会通过以下方式解决您的问题.

This may look slight more complicated then it seems, but I would work out your issue in the following way.

  1. C:\Windows\System32\drivers\etc\hosts 文件上创建一个 dev 域,指向 IP 127.0.0.1,它是本地主机 IP.

  1. Create a dev domain on the C:\Windows\System32\drivers\etc\hosts file pointing to the IP 127.0.0.1 which is the localhost IP.

域名并不重要,只是您可以在给定项目中使用的简单名称,例如 test.dev,因此主机文件将如下所示:

The domain name doesn't really matter just something easy for you to use for that given project for example test.dev so the hosts file would look like this:

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost
127.0.0.1   localhost
127.0.0.1   test.dev
127.0.0.1   www.test.dev

  • 现在定义该项目的文件夹所在的位置,我将在本示例中使用 c:\projects\test.

    现在在您的网络服务器上为您刚刚创建的域创建一个新的 virtualhost,这是我使用的 virtualhost 示例:

    Now create on your web server a new virtualhost for the domain you have just created, here is a sample of virtualhost that I use:

    <VirtualHost *:80>
            ServerAdmin root@localhost
            DocumentRoot "C:/projects/test"
            ServerName test.dev
            ServerAlias www.test.dev
            ErrorLog "C:/projects/logs/test.dev_error_log"
            CustomLog "C:/projects/logs/test.dev_access_log" common
            RewriteLog "C:/projects/logs/test.dev_rewrite_log"
            RewriteLogLevel 9
            ScriptAlias /cgi-bin/ "C:/projects/test/cgi-bin/"
    
            <Directory "C:/projects/test">
                    Options -Indexes FollowSymLinks +ExecCGI
                    AllowOverride All
                    Order allow,deny
                    Allow from all
            </Directory>
            <Directory "C:/projects/test/cgi-bin">
                    AllowOverride None
                    Options None
                    Order allow,deny
                    Allow from all
            </Directory>
    </VirtualHost>
    

  • 重新启动服务器以激活更改,主机文件无需重新启动计算机,保存后应立即生效.

  • Restart your server to activate the changes, you do not need to restart the computer for the hosts file, it should work immediately once you save it.

    现在您可以使用 http://www.test.devhttp://test.dev 并且您可以使用完全相同的 .htaccess 在开发和实时站点上.

    Now you can use http://www.test.dev or http://test.dev and you can use the very same .htaccess on both, dev and live sites.

    <小时>

    其他一些提示:


    Some other tips:

    1. 总是使用和定义 RewriteBase/ 因为当你想在开始使用没有 / 的规则时它会产生特别的影响,就像你提到的对于 RewriteRule ^example/$example.php [L,NC]

    1. Always use and define RewriteBase / as it can make a difference specially when you want to use rules without the / at the start, like you have mentioned for RewriteRule ^example/$ example.php [L,NC]

    根据您要使用的规则,您可能需要指定 -MultiViews 所以我经常默认保持启用状态,如下所示:

    Depending on the rules you want to use, you may need to specify -MultiViews so I often just leave it on by default, like this:

    Options +FollowSymLinks -MultiViews
    

  • 正如您在我的 virtualhost 设置中看到的那样,我默认将访问、错误和重写日志定义到日志文件夹中,并使用有问题的域名,以便当我需要查看发生了什么时我可以轻松访问该域的清晰日志,而不是我在同一 virualhost 上运行的所有项目.
  • As you can see on my virtualhost setup, I define the access, error and rewrite logs into the logs folder by default and with the domain name in question so when I need to see what is going on I have an easy access with the clear logs for that domain only and not my whole bunch of projects running on the same virualhost.
  • 我想就是这样,你真的不需要那个巨大的 virtualhost 这就是我使用它的方式,它不像我需要一直复制和粘贴它,因为我有我的自己的简单脚本来创建新域并为我完成所有工作.

    I guess that is about it, you don't really need that huge virtualhost that's just how I use it and its not like I need to copy and paste it all the time as I have my own simple script to create new domains and do all the work for me.

    这篇关于在开发和实时服务器之间切换时不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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