【问题标题】:Localhost and htaccess本地主机和 htaccess
【发布时间】:2014-07-13 09:18:29
【问题描述】:

我的站点文件夹中列出了五个目录,它们充当本地服务器上的站点。此类站点之一使用 codeigniter 进行编码。我违背了自然文件结构,并将应用程序和系统文件夹移到了站点文档根目录之外。我目前有运行驻留在 public_html 中的应用程序所需的 index.php 文件,以便在准备好进行测试时轻松传输到我的实时站点。当我在本地服务器上时,如何删除 URL 的所有额外部分。

现在我将它显示为:

本地主机/站点名称/public_html/控制器

我希望对这个网站做的设置是,当我在浏览器中打开它时,它会显示如下内容:

dev.mysite.com/controller.

它知道 public_html 只是一个文件夹。

我在 htaccess 中做什么?

更新:

在我的 Mac 上,我有一个站点文件夹,其中包含我本地服务器上的所有站点。

- Sites
    -site1
        -application
        -public_hml
            index.php
            .htaccess
        -system
    -site2
        -application
        -public_html
            index.php
            .htaccess
        -system

我现在已经将 mamp 中的 apache 设置为站点是文档根目录的位置。这是我应该做的吗?

当我想加载页面的索引文件时,它显示为:

localhost/site1/public_html/index.php

显然,如果我要将它上传到实时服务器,我所要做的就是转到以下链接,它会起作用。我只是在上传到实时服务器之前尝试在本地开发。

sitesname.com/index.php

【问题讨论】:

标签: php .htaccess codeigniter


【解决方案1】:

我相信我过去曾这样做过。从 public_html 文件夹中移动 ApplicationSystem 文件夹是一种最佳做法。它将阻止这两个文件夹从公共直接访问。移动文件夹后,您必须在 public_html\index.php 文件中进行更改。将系统和应用程序路径指向上一级。请参考下面的图片,看看我是如何编辑的。

http://i.stack.imgur.com/ekQKi.jpg

执行上述编辑后,现在您的应用程序可以正常工作,但您必须在 URI 中包含 index.php 段。例如:- localhost/index.php/welcome。要摆脱这种情况,您必须在 public_html 文件夹中创建一个 .htaccess 文件。请参考下图。

http://i.stack.imgur.com/meF6Z.jpg

现在为了您的方便,我附上下面的重写代码。

<IfModule mod_rewrite.c>

Options +FollowSymLinks
RewriteEngine on

# Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule> 

希望您的网站能够在 localhost/welcome 等 URI 中不包含 index.php 的情况下运行

当我想加载页面的索引文件时,它显示为: localhost/site1/public_html/index.php 显然,如果我要将它上传到实时服务器,我所要做的就是转到 >>以下链接,它会起作用。我只是在上传到实时服务器之前尝试在本地开发。 sitesname.com/index.php

请注意,在您的实时服务器上,域名 sitename.com 也指向一个本地目录结构,例如 localhost/site1/public_html/。您也可以在本地主机中实现相同的目标。例如,您可以将您的开发站点设置为像 yoursite.dev/index.php 这样的 URL。为此,您必须执行两件事。

  1. 在您的本地 Apache 服务器中创建一个站点(将其命名为 yoursite.dev)
  2. 编辑主机文件以解析 yoursite.dev

虽然我使用的是ubuntu机器来演示配置,但我相信你也可以在mac上弄清楚

在 Apache 中创建站点

sudo nano -w /etc/apache2/sites-available/yoursite.conf

将以下内容粘贴到文件中并进行必要的更改

<VirtualHost *:80>
    ServerName yoursite.dev
    ServerAlias *.yoursite.dev
    DocumentRoot /path/to/public_html/folder

<Directory />
    #Options FollowSymLinks
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>

</VirtualHost>

保存文件。现在您已经创建了配置文件并需要启用该站点。

sudo a2ensite sandbox
service apache2 restart

编辑主机文件以解析 yoursite.dev

sudo nano -w /etc/hosts

将以下行添加到最后并保存文件

127.0.0.1       yoursite.dev

如果您一直关注我,现在您将能够使用以下 URL 访问您的开发站点:yoursite.dev/index.php

希望对您有所帮助,如果您有任何疑问,请告诉我。

【讨论】:

    【解决方案2】:

    我在 WINDOWS 上的 XAMPP 中工作

    转到C:\Windows\System32\drivers\etc\,然后在文本编辑器中打开名为hosts 的文件。然后在文件末尾添加这行

    127.0.0.1 sitesname.com sitesname2.com sitesname3.com 
    

    例如:

    之后转到XAMPP安装位置

    对我来说:E:\xampp。然后转到其中的文件夹apache\conf\extra。找到名为httpd-vhosts.confE:\xampp\apache\conf\extra\httpd-vhosts.conf)的文件

    然后添加

    <VirtualHost *>
        DocumentRoot "E:\xampp\htdocs\Sites\site1"
        ServerName sitesname.com
    <Directory "E:\xampp\htdocs">
        Order allow,deny
        Allow from all
    </Directory>
    

    <VirtualHost *>
        DocumentRoot "E:\xampp\htdocs\Sites\site2"
        ServerName sitesname2.com
    <Directory "E:\xampp\htdocs">
        Order allow,deny
        Allow from all
    </Directory>
    

    例如:

    保存文件并重新启动Apache 服务器并检查sitesname.com

    它对我有用。

    注意:有时在 E:\xampp\htdocs\ 之外添加的文件夹无法被 Apache 访问

    【讨论】:

      【解决方案3】:

      您需要在路线中更改它

      [yournameyouwant] = "控制器/索引";

      [yournameyouwant] = "文件夹/控制器/索引";

      这是我使用的一些 htaccess

      Options +FollowSymLinks
      Options -Indexes
      DirectoryIndex index.php
      RewriteEngine on
      RewriteCond $1 !^(index\.php|images|robots\.txt)
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php/$1 [L,QSA]
      

      或者

      <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteBase /
      
          #Removes access to the system folder by users.
          #Additionally this will allow you to create a System.php controller,
          #previously this would not have been possible.
          #'system' can be replaced if you have renamed your system folder.
          RewriteCond %{REQUEST_URI} ^system.*
          RewriteRule ^(.*)$ /index.php?/$1 [L]
      
          #When your application folder isn't in the system folder
          #This snippet prevents user access to the application folder
          #Submitted by: Fabdrol
          #Rename 'application' to your applications folder name.
          RewriteCond %{REQUEST_URI} ^application.*
          RewriteRule ^(.*)$ /index.php?/$1 [L]
      
          #Checks to see if the user is attempting to access a valid file,
          #such as an image or css document, if this isn't true it sends the
          #request to index.php
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php?/$1 [L]
      </IfModule>
      
      <IfModule !mod_rewrite.c>
          # If we don't have mod_rewrite installed, all 404's
          # can be sent to index.php, and everything works as normal.
          # Submitted by: ElliotHaughin
      
          ErrorDocument 404 /index.php
      </IfModule>
      

      【讨论】:

        【解决方案4】:

        您可能会在 C:\Windows\System32\drivers\etc\ 看到 hosts 文件。

        使用编辑器,您必须添加以下代码:

        127.0.0.1  local.site1.com
        127.0.0.1  local.site2.com
        

        之后,你必须在 C:\xampp\apache\conf\extra\httpd-vhosts.conf 中添加这个

        <VirtualHost *>
            DocumentRoot "C:\xampp\htdocs\Sites\site1"
            ServerName local.site1.com
        </VirtualHost>
        <VirtualHost *>
            DocumentRoot "C:\xampp\htdocs\Sites\site1"
            ServerName local.site2.com
        </VirtualHost>
        

        然后,重新启动 xampp。 您需要编辑每个站点的 .htaccess 文件。

        RewriteEngine on
        RewriteBase /site1/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
        
        RewriteEngine on
        RewriteBase /site2/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
        

        完成

        【讨论】:

        • 我在 Mac 上,所以我在哪里输入 127.0.0.1 local.site1.com 127.0.0.1 local.site2.com at?
        • @user2576961 您可以在主机文件中输入它。使用命令sudo nano /private/etc/hosts
        【解决方案5】:
        • 在 apache 中设置一个连接到本地文件夹的虚拟主机 (my.local.dev)。
        • http://my.local.dev 添加到您的主机文件中,诱使您的操作系统使用它。
        • (重启apache)
        • 仍然失败? htaccess 在您的公共 html 中或更改 codeigniter 路由

        【讨论】:

          猜你喜欢
          • 2015-08-08
          • 2021-11-20
          • 2015-10-15
          • 2017-09-05
          • 2010-11-19
          • 2013-01-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多