Welcome Guest, Not a member yet? Register   Sign In
friendly url to css, images, js and etc. folders using routes.php ?
#1

[eluser]ruby42[/eluser]
Hi,

I'd like to use routes.php file to put on my website friendly url to these folders: css, images, js, etc. and I don't want to move my assets folder to the root directory - I know that I can move this folder to the root directory and add assets to .htaccess file and it will work correctly, but I would not like to change my structure.

At this moment my url is: www.mypage.com/system/application/assets/images/top.png
but I would like to have this: www.mypage.com/images/top.png

Structure of my folders is:
Code:
/root
    /system
        /application
            /assets
                /css
                /images
                /javascript
/.htaccess

My .htaccess is:
Code:
RewriteEngine On
RewriteCond $1 !^(index.php|system|robots.txt|sitemap.xml|favicon.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]


Something like this doesn't work:
Code:
$route['images/(:any)'] = 'system/application/assets/images/$1';

Thanks for helping.
#2

[eluser]keevitaja[/eluser]
move your assets folder to /assets (same dir where .htaccess is) and change your .htaccess file:

Code:
RewriteEngine On
RewriteCond $1 !^(index.php|system|robots.txt|sitemap.xml|favicon.ico|assets)
RewriteRule ^(.*)$ /index.php/$1 [L]


no additional routing is needed!

it seems to be an old CI version you are using. not sure if system folder has to be in .htaccess
#3

[eluser]ruby42[/eluser]
it is old version and everything work correctly. I know that I can move this folder assets to root folder and add this line to /htaacess, but I don't want to do this. I would like to use routes.php file, but I don't know if it possible?
#4

[eluser]CroNiX[/eluser]
Why would you want to use CI's resources to display simple files? If you use routing, that means you are using CI to pass the request to a controller to process it. So you'd have to manually load the asset in that controller from the filesystem and output it along with the correct headers for that asset type. This can put quite a load on your server for each asset requested (at least 6mb used per request) instead of the webserver, not CI, sending a simple file very efficiently, the way it was designed to.

In order for your controller to work with your routing, your image src needs to be something like

Code:
<img src="&lt;?php echo base_url('images/image_name.jpg'); ?&gt;" alt="" />
#5

[eluser]ruby42[/eluser]
I thought that I was able to use routing from CI like it works in htaccess file = request will not be sending to controller, but it will just rewrite my rule. Now I see that routing in CI is useful when I'm sending something to controller (and I want to use this dynamically) as for example:

Code:
$route['([a-zA-Z]{2})/([a-zA-Z0-9-]+)/([0-9]+)-([0-9]+)-([a-zA-Z0-9-]*)'] = 'ads/ads_show_subcategory/$3/$4';

So, I will try to use htaccess file to rewrite my rule, because it works before request is sending to CI - what do you think about this? Will it work correctly?

my .htaccess looks like:
Code:
RewriteEngine On

RewriteRule ^assets/ads-images/([^/]*)$ /system/application/assets/ads-images/$1 [L]
RewriteRule ^assets/banners/([^/]*)$ /system/application/assets/banners/$1 [L]
RewriteRule ^assets/css/([^/]*)$ /system/application/assets/css/$1 [L]
RewriteRule ^assets/flash/([^/]*)$ /system/application/assets/flash/$1 [L]
RewriteRule ^assets/images/([^/]*)$ /system/application/assets/images/$1 [L]
RewriteRule ^assets/javascript/([^/]*)$ /system/application/assets/javascript/$1 [L]
RewriteRule ^assets/scripts/([^/]*)$ /system/application/assets/scripts/$1 [L]
RewriteRule ^assets/site-images/([^/]*)$ /system/application/assets/site-images/$1 [L]

RewriteCond $1 !^(index.php|system|robots.txt|sitemap.xml|sitemap.xml.gz|sitemap_pl.xml|sitemap_pl.xml.gz|sitemap_ru.xml|sitemap_ru.xml.gz|sitemap_en.xml|sitemap_en.xml.gz|sitemap_es.xml|sitemap_es.xml.gz|favicon.ico|websites|prolink.php|prolink_config.php|prolink_9e1483f6d0ba91ece177d147bd77511f.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]




Theme © iAndrew 2016 - Forum software by © MyBB