修改两个站点的htaccess文件

发布于 2024-10-10 10:24:48 字数 482 浏览 0 评论 0原文

我的网络主机将我的“主”域名指向根 www 文件夹。该站点的 Web 文件位于“www/app/webroot”文件夹中。我目前使用 htaccess 文件中的以下内容启动并运行该站点:

RewriteBase /
RewriteEngine on
RewriteRule    ^$ app/webroot/    [L]
RewriteRule    (.*) app/webroot/$1 [L] 

我正在尝试为同一站点启动一个开发站点。我在 www 文件夹中创建了一个名为“dev”的文件夹。因此,该文件夹的 Web 文件位于:“www/dev/app/webroot” 我有一个指向 dev 文件夹的子域。当我在 dev 文件夹中使用与上面相同的 htaccess 时,它不起作用,因为(我相信)它继承了根 www 文件夹的设置。当页面加载时,它只是显示空白。如何设置 htaccess 文件以允许这两个站点?

预先感谢您的任何帮助!我显然是这方面的新手。

My web host points my "main" domain name to the root www folder. The web files for that site are located in the "www/app/webroot" folder. I currently have the site up and running using the following in the htaccess file:

RewriteBase /
RewriteEngine on
RewriteRule    ^$ app/webroot/    [L]
RewriteRule    (.*) app/webroot/$1 [L] 

I'm trying to start a dev site for the same site. I made a folder named "dev" in the www folder. So, the web files for this folder are in: "www/dev/app/webroot" I have a sub-domain pointing to the dev folder. When I use the same htaccess as above in the dev folder, it doesn't work because (I believe) it is inheriting the settings from the root www folder. When the page loads, it just comes up blank. How do I set up my htaccess files to allow for both sites?

Thanks in advance for any help! I'm obviously a novice at this stuff.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮云壑 2024-10-17 10:24:49

所以我们会尝试清理这些东西:-)

避免使用.htaccess。目录 /foo/bar 中 .htaccess 中的所有设置都可以在 apache 配置中设置为目录设置(如果您在 apache conf 上提供有限访问,则 .haccess 很有用,如果您拥有服务器,则不要使用它)。

<Directory /foo/bar>(...)</Directory>

然后您可以使用基于命名的虚拟主机访问您的站点。验证您是否有此选项:

NameVirtualHost *:80

当您拥有它时,美好的事情就可以开始了。
这将是您的第一个应用程序的虚拟主机:

<VirtualHost *:80>
    ServerName app
    ServerAlias www.app.somwhere.com
    ServerAlias app.somwhere.com
    DocumentRoot /www/app/webroot
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /www/app/webroot>
        Options Indexes FollowSymLinks
        # this prevent.htaccess reading, remove if you want .htaccess
            AllowOverride None
            # allow web access 
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

大多数 apache 设置可以在此处定义。仅适用于您的第一个应用程序。 Apache 将为站点名称“app”、“www.app.somwhere.com”或“app.somwhere.com”完成的所有请求提供此配置。您可以定义很多别名(ServerAlias),但只能定义一个名称(ServerName)。

然后,如果您进入浏览器并输入 http://app/ 您的浏览器将找不到服务器,因此将其设置为你的/etc/hosts.这是每个想要访问您的应用程序的人都应该在主机文件中拥有的内容,直到您获得真正的 DNS(假设您的第一个应用程序是 app.somwhere.com,第二个应用程序是 foo.somwhere.com,92.128.52.226 是您的外部 IP) :

127.0.0.1 app.somwhere.com app foo foo.somewhere.com
92.128.52.226 app.somwhere.com app foo foo.somewhere.com

现在让我们为您的第二个应用程序添加另一个虚拟主机:

<VirtualHost *:80>
    ServerName foo
    ServerAlias www.foo.somwhere.com
    ServerAlias foo.somwhere.com
    DocumentRoot /www/foo/webroot
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /www/foo/webroot>
        Options Indexes FollowSymLinks
        # this prevent.htaccess reading, remove if you want .htaccess
            AllowOverride None
            # allow web access 
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

等等。
不要忘记重新启动你的 apache。没有重写规则。良好的虚拟主机是良好配置的第一步,您将能够根据所使用的名称定义规则、目录或位置特定的内容。甚至可以使用 php_value 设置每个虚拟主机的 php 配置,而不是在 php.ini 上设置全局共享配置。

输入

apache2 -S

以获取虚拟主机列表,您将看到第一个是“默认”主机,如果 apache 不理解所请求站点的名称,它将提供此默认主机(因此您可以添加特定的虚拟主机)在顶部处理这些情况)。

So we'll try to clean the things :-)

Avoid using .htaccess. All the settings in a .htaccess in a directory /foo/bar can be set in apache configuration as a Directory setting (.haccess is usefull if you provide limited access on apache conf, if you own the server don't use it).

<Directory /foo/bar>(...)</Directory>

Then you can access your sites with named based virtualhosts. Verify you have this option:

NameVirtualHost *:80

When you have it nice things can start.
This will be your virtualhost for your 1st app:

<VirtualHost *:80>
    ServerName app
    ServerAlias www.app.somwhere.com
    ServerAlias app.somwhere.com
    DocumentRoot /www/app/webroot
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /www/app/webroot>
        Options Indexes FollowSymLinks
        # this prevent.htaccess reading, remove if you want .htaccess
            AllowOverride None
            # allow web access 
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

Most apache settings can be define here. Only for your 1st app. Apache will serve this configuration for all requests done for the site name 'app', or 'www.app.somwhere.com', or 'app.somwhere.com'. You can define a lot of alias(ServerAlias)., and only one name (ServerName).

Then if you go in your browser and type http://app/ your browser won't find the server, so set it in your /etc/hosts. This is what every people wanting to access your app should have in the hosts file until you get a real DNS (assuming your 1st app is app.somwhere.com and the second foo.somwhere.com and 92.128.52.226is your external IP):

127.0.0.1 app.somwhere.com app foo foo.somewhere.com
92.128.52.226 app.somwhere.com app foo foo.somewhere.com

And now let's add another virtualhost for your second app:

<VirtualHost *:80>
    ServerName foo
    ServerAlias www.foo.somwhere.com
    ServerAlias foo.somwhere.com
    DocumentRoot /www/foo/webroot
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /www/foo/webroot>
        Options Indexes FollowSymLinks
        # this prevent.htaccess reading, remove if you want .htaccess
            AllowOverride None
            # allow web access 
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

And etc.
Don't forget to restart your apache. No rewrite rule. nice virtualhosts is the 1st step of a nice configuration, you will be able to define rules, directory or location specific things per name used. Even php configuration can be set per virtualhost with php_value instead of a global shared one on php.ini.

type

apache2 -S

to get the list of your virtualhosts, you'll see that the first one is the 'default' one, if apache does'nt understand the name of the requested site it will serve this default one (so you could ad a specific virtualhost on top to handle theses cases).

随心而道 2024-10-17 10:24:49

尝试将 dev/ 添加到 dev.htaccess 的第 3 行和第 4 行中的路径。

Try adding dev/ to the paths in lines 3 and 4 to your dev .htaccess.

反目相谮 2024-10-17 10:24:49

也许您应该删除 dev 文件夹中 .htaccess 中的“RewriteBase /”行?

Maybe you should remove the "RewriteBase /" line in the .htaccess in your dev folder?

小镇女孩 2024-10-17 10:24:49

我还设法使用 Xammp 托管多个项目文件夹。

我首先关注 YouTube 视频。观看视频后,我找不到视频中引用的主机文件,因此我查看了 stackoverflow,其中 Maytham Fahmi 对“如何在 XAMPP (Windows) 上设置 Apache 虚拟主机 [已关闭]”的回答

我使用以下来源添加虚拟主机:

I have also managed to host several project folders with Xammp.

I started by following the YouTube video. After watching the video I couldn't find the hosts file referenced in the video so I looked on stackoverflow where Maytham Fahmi's answer to "How To Set Up Apache Virtual Hosts on XAMPP (Windows) [closed]"

I used the following sources to add virtual hosting:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文