簡體   English   中英

為什么.htaccess網址重寫無法正常工作的Codeigniter模板?

[英]Why is .htaccess url rewrite not working codeigniter mamp?

[edit]: 如何在codeigniter的路徑中刪除“ index.php”的副本
[那里的解決方案對我不起作用]

我第一次嘗試codeigniter,對php來說還是很新的。 我想從我的網址中刪除index.php。

安裝的代碼點火器
用我自己的替換了index.php

我的.htaccess中有這個

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

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


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

我的重寫是絕對的,我在根目錄中加載了phpinfo頁面,它顯示了已加載的模塊。 Ive還檢查了httpd.conf

當我從NetBeans運行時,我被定向到localhost:8888 / domain / index.php並且我的索引頁面加載
當我轉到localhost:8888 / domain時,我的index.php也將加載

當我轉到localhost:8888 / domain / welcome或localhost:8888 / domain / welcome.php時我得到404
The requested URL /index.php/welcome was not found on this server.
當我轉到localhost:8888 / domain / index.php / welcome時,我被定向到Welcome控制器,但它僅加載我的index.php但沒有標記。

我已經在.htaccess中嘗試過此操作:

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php/$1 [QSA,L] 

我得到:

在這里輸入代碼

這也沒有用(我知道,他們都打算做幾乎相同的事情):

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteBase /
    # Hide the application and system directories by redirecting the request to index.php
    RewriteRule ^(application|system|\.svn) index.php/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,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>

在配置/ config.php文件:

 $config['base_url']    = 'localhost:8888/domain.com/';
    $config['index_page'] = '';

我用默認的CI索引替換了自己的索引(也就是說,我將其返回到開箱即用的狀態)。
然后,我將domain/index.php/pages/view/page.phpdomain/index.php/page.php
然后,我運行了Nishant的.htaccess代碼,盡管它似乎無法正常工作(網站的靜態非樣式版本顯示在通常的url上,但是沒有index.php的url上卻沒有顯示),但它可能幫助我進入了解。
我也將RewriteEngine On更改為RewriteEngine on盡管我非常確定我之前已經嘗試過。 在完成這些步驟之后,我又跳回了sbaaaang建議的代碼(該代碼以前曾嘗試失敗,但有些版本ID),並且這次可以正常工作了。

對於可能會遇到.htaccess麻煩的任何其他人,我建議您在開箱即用時將它們放下並經過tutes(而不是像我一樣跳入其中)。

我的.htaccess最終像這樣

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  


<Files "index.php">
AcceptPathInfo On
</Files>  
</IfModule>

<IfModule !mod_rewrite.c>

ErrorDocument 404 /index.php
</IfModule>

和配置變量如:

$config['base_url'] = '';
$config['index_page'] = '';
 $config['base_url']    = 'http://'.$_SERVER['HTTP_HOST'].'/';
 $config['index_page'] = '';

然后.htaccess:

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

我也注意到您說的“用我自己的替換了index.php”,這是錯誤的,將原始Codeigniter index.php文件保留在主根目錄中,否則您將永遠不會運行codeigniter:D

希望這個能對您有所幫助

您面臨的問題是因為您刪除了index.php並將其替換為您自己的index.php。

而是將codeigniter文件夾放在本地主機上,並將index.php放在[本地主機的根目錄下],然后在瀏覽器中打開以下網址。

http://localhost:8888/domain/index.php/welcome

如果工作正常,則意味着顯示您的代碼初始化工具的歡迎頁面,那么您可以考慮通過添加以下內容從網址中刪除index.php

RewriteEngine on
RewriteCond $1 !^(index\.php|static)
RewriteRule ^(.*)$ /index.php/$1 [L]

在您的.htaccess文件中,重寫條件的第二行將確保重寫規則不會執行,當url中包含index.php或static時,可以將所有靜態內容放在static文件夾中。

現在,當網址中沒有index.php或static時,重寫規則將對其進行重寫。

假設網址是

http://localhost:8888/domain/welcome

將被重寫為

http://localhost:8888/domain/index.php/welcome

在每種情況下,您的代碼都將僅通過包含index.php的url提供。

試試這個非常簡單的代碼,對我來說效果很好:

放置項目文件夾而不是project2的名稱更改配置base_url =“”

RewriteEngine on
RewriteCond $1 !^(index\.php|uploads|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /project2/index.php/$1 [L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM