Я использую следующее htaccess
сценарий так, чтобы я мог скрыться index.php
от URI.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Но я сталкиваюсь с основной проблемой :(
Мне назвали каталог assets
около моего index.php
файл и это должны быть там. Когда я просматриваю каталог браузером, затем Codeigniter, не найденный отображениями страницы. Я не могу просмотреть файл /assets/image.jpg
но это отображается, когда я называю его от <img>
тег
Что я могу сделать теперь?
Обратите внимание, что это работает в моем локальном сервере (localhost
) но не в живом сервере.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php/$1 [NC,L]
Это передаст любой запрос, который Apache не будет обслуживать, в CodeIgniter. Таким образом, вы можете создавать каталоги повсюду, не обновляя свои правила перезаписи, и вам не нужно настраивать обработку 404 в Apache (даже внутри ваших каталогов изображений / ресурсов).
Благодарим авторов Zend Framework, которые рекомендуют этот бит в своем руководстве пользователя. Эта версия немного модифицирована для CodeIgniter.
Вот файл .htaccess, который я использую для своей установки CI:
Если он находится в подпапке, вам нужно будет отредактировать RewriteBase.
<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>
Вот полное решение, которое я использую и которое прекрасно работает:
Поместите этот фрагмент кода ниже в файл .htaccess в корень вашего проекта, например http: // localhost / myProject
RewriteEngine On
RewriteBase /appDirName
RewriteCond %{REQUEST_URI} ^appDirName.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
И в вашем CI config, расположенный в system \ application \ config \ config.php
$config['uri_protocol'] = "REQUEST_URI";
# Turn on URL rewriting
RewriteEngine On
# Put your installation directory here:
RewriteBase /
# Allow these directories and files to be displayed directly:
# - index.php (DO NOT FORGET THIS!)
# - robots.txt
# - favicon.ico
# - Any file inside of the images/, js/, or css/ directories
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|images|js|css)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
Я использую это, и он отлично работает!
Вставьте следующее в ваш .htaccess
# Change "welcome" to your default controller:
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/$ $1 [L,R=301]
# Checks to see if the user is attempting to access a valid file,
# if not send them to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]