[英]Localhost and htaccess
我的網站文件夾中列出了五個目錄,它們充當我本地服務器上的網站。 其中一個站點使用codeigniter進行編碼。 我違反了自然文件結構,並將應用程序和系統文件夾移到了站點文檔根目錄之外。 我目前有一個index.php文件需要運行駐留在public_html中的應用程序,以便在准備好測試時輕松轉移到我的實時站點。 當我在我的本地服務器上時,如何刪除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,其中site是文檔根目錄。 這是我應該做的。
當我想加載頁面的索引文件時,它顯示為:
localhost/site1/public_html/index.php
顯然,如果我要將其上傳到實時服務器,我所要做的就是轉到以下鏈接,它會起作用。 我只是在上傳到實時服務器之前嘗試在本地開發。
sitesname.com/index.php
我相信我過去做過這件事。 從public_html文件夾移動Application和System文件夾是最佳做法。 它將阻止這兩個文件夾公開直接訪問。 移動文件夾后,您必須在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。 要做到這一點,你必須執行兩件事。
雖然我使用的是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
我希望這對您有所幫助,如果您有任何疑問,請告訴我。
我在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.conf
的文件( E:\\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訪問
您需要在路線中更改它
[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>
您可能會在C:\\ Windows \\ System32 \\ drivers \\ etc \\中看到主機文件。
使用編輯器,您必須添加以下代碼:
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]
完成
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.