Localhost和htaccess

I have five directories listed inside my sites folder that act as sites on my localserver. One of such sites is coded with codeigniter. I have gone against the natural file structure and have moved the application and system folders outside the site document root. I currently have the index.php file needed to run the application residing in the a public_html for easy transfer to my live site when it's ready for testing. While I'm on my local server how to I remove all of the extra parts of the URL.

Right now I have it showing up as :

localhost/sitename/public_html/controller

What I would prefer to do with this site is set it up so that when I open it in my browser it'll show so something like :

dev.mysite.com/controller.

As have it know the public_html is just a folder.

What sort of things in htaccess do I do?

UPDATE:

On my mac I have a sites folder and that houses all my sites on my local server.

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

I have right now have the apache in the mamp set up to where sites is the document root. Is this what I should do.

When I want to load the index file of a page it shows up as:

localhost/site1/public_html/index.php

Obviously if I were to upload this to a live server all I would have to do is go to the following link and it would work. I'm just trying to develop locally before I upload to a live server.

sitesname.com/index.php

You will need to change it in your routes

[yournameyouwant] = "controller/index";

[yournameyouwant] = "folder/controller/index";

Here are some htaccess I use

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]

Or

<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>

I am working in XAMPP on WINDOWS

Go to C:\Windows\System32\drivers\etc\ then open the file named hosts in text editor. Then add this lines at the end of the file

127.0.0.1 sitesname.com sitesname2.com sitesname3.com 

Eg : hosts file content

After that go to XAMPP installed location

For me its : E:\xampp . Then go to folder apache\conf\extra in it. Find the file named httpd-vhosts.conf ( E:\xampp\apache\conf\extra\httpd-vhosts.conf )

then add

<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>

Eg : vertual host setup

Save the file and restart the Apache server and check sitesname.com.

It works for me.

Note : Sometimes Folder added outside E:\xampp\htdocs\ can't accessed by Apache

I believe i did this in the past. Moving the Application and System folder from public_html folder is a best practice. It will prevent those two folders from public direct access. After moving the folders you have to make changes in the public_html\index.php file. Point the system and application path one level up. Please refer to the picture below to see how i edited.

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

After performing the above edit, now your application will work fine but you have to include the index.php segment in the URI. eg:- localhost/index.php/welcome. To get rid that, you will have to create an .htaccess file inside the public_html folder. please refer to the picture below.

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

Now for your convenience, I'am attaching the rewrite code below.

<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> 

Hopefully your site will work without including the index.php in the URI like localhost/welcome

When I want to load the index file of a page it shows up as: localhost/site1/public_html/index.php Obviously if I were to upload this to a live server all I would have to do is go to the >>following link and it would work. I'm just trying to develop locally before I upload to a live server. sitesname.com/index.php

Please be noted that, on your live server the domain name sitename.com is also pointing to a local directory structure like localhost/site1/public_html/. You can achieve the same in your localhost too. For example you can set your development site available to a URL like yoursite.dev/index.php. To do that you have to perform two things.

  1. Create a site in your local Apache server (name it as yoursite.dev)
  2. Edit the host file to resolve the yoursite.dev

though i am using an ubuntu machine to demonstrate the configuration, I believe you could figure it out on the mac too

Creating a site in Apache

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

paste the following into the file and make necessary changes

<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>

Save the file. Now you have created the configuration file and need to enable the site.

sudo a2ensite sandbox
service apache2 restart

Edit the host file to resolve the yoursite.dev

sudo nano -w /etc/hosts

add the following line to the end and save the file

127.0.0.1       yoursite.dev

If you followed me along, now you will be able to access your development site by using the following URL: yoursite.dev/index.php

I hope that will help you and please let me know if you have any queries.

You may see hosts file at C:\Windows\System32\drivers\etc\ .

Using editor, you have to add following code:

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

After that, you must add this at 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>

And, restart xampp. You need to edit .htaccess file of each site.

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]

Done

  • Setup a virtual host (my.local.dev) in apache that connects to your local folder.
  • Trick your OS into using http://my.local.dev by adding it to your hosts file.
  • (restart apache)
  • Still failing? htaccess in your public html or change codeigniter routing