Using mod_rewrite to access images better? - mod-rewrite

In my application, my javascript files are in the folder /web_root/inc/js/, my images are in /web_root/inc/images/, and my css files are in /web_root/inc/css/.
I don't like the links, so I thought I'd use mod_rewrite on them. Is it recommended and/or possible to use mod_rewrite, so in my code I could include images, JS and CSS files just like in the following example:
<img src="images/img.png" alt="image" />
Instead of:
<img src="inc/images/img.png" alt="image" />

In your web_root folder add an .htaccess file and include the next rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^images/(.*) http://%{HTTP_HOST}/inc/images/$1 [R=301,L]
</IfModule>
I did it and it worked for me.
Hope i help you

Related

Laravel root directory not public on Hostinger hosting

I currently host my Laravel website on Hostinger, but when I have a image on my page, the url doesn't go to website.com/public/img_url, but it goes to website.com/img_url, does anyone know how this works on Hostinger, and how I can fix this?
Currently my whole project is in my public_html, with no other folders besides it.
The right solution was changing my htaccess to:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
You should need to add a public inside asset function to access a public folder on the shared hosting server.
So you need to change the fetch path of files Example: asset('user/photo.jpg') to asset('public/user/photo.jpg') in each line of code where you need to fetch files from the public directory.

Magento Redirect base url

I have all my folders and files in www.mysite.com/magento/ and my home page base url is www.mysite.com/magento/. I need to change this url to only www.mysite.com and if possible I don't wanna move my files from /magento/ to the back folder.
If I use System - Configuration - Web from my magento-admin-panel, it doesn't work and I can't connect to panel.
You should be able to accomplish that using .htaccess and RewriteBase.
Here's commented out example in Magento .htaccess sample:
############################################
## you can put here your magento root folder
## path relative to web root
RewriteBase /magento/
That should do the trick in your case.
You can read more about RewriteBase here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#page-header
Edit: In the web root, you'll need some sort of rewrite to Magento root directory:
RewriteEngine on
RewriteCond %{REQUEST_URI} !magento/
RewriteRule (.*) /magento/$1 [L]
Keep in mind that there are all just untested examples that should point you in the right direction. :)
Cheers.

How to access public folder in laravel 4?

I was trying to add some styling in my localhost(wamp) lavarel project....
This is my style.css file located in public folder
p{font-weight:bold; size:20px; color:red;}
This is hello.blade.php file located in app/view folder
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="{{ URL::asset('style.css') }}">
</head>
<body>
<p>Test</p>
</body>
</html>
& My routes.php file is having following code:
Route::get('/',function(){
return View::make('hello');
});
.htaccess file located in root directory is
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
When I am trying to access that hello page by localhost, Test content is visible without any styling. Can anyone tell me what is going wrong with me??
Your directory structure is different from the default one shown here
You have to put the packages folder and these files into the public directory:
.htaccess
favicon.ico
index.php
robots.txt
Then to make your site work again change the document root of your apache server to path/to/application/public
There's a reason for the public directory. When you put it on a server on the internet you gain additional security because the user can only access what is in the public directory. (because the domain is pointing to this folder) If you use your current setup everybody could see your whole application. For example one could enter example.com/app/config/database.php and would see your database password.
Put it like this:
<link rel="stylesheet" href="{{ asset('style.css') }}">
Without URL::

Where do javascript and css files go in a laravel project

Where should static javascript and css files in a Laravel 4 application. For the sake of my own understanding, I'm not interested in using any sort of assets or packages yet.
I tried putting them under public/js and public/packages to no avail, getting the following 404 errors in my page:
GET http://localhost/~myuser/mytest/packages/test2.js 404 (Not Found)
GET http://localhost/~myuser/mytest/js/mytest.js 404 (Not Found)
Here is what my .htaccess file looks like for reference:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /~myuser/mytest/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
All assets should be placed in the public folder
public/js
public/css
public/fonts
public/images
You can access the assets using the asset() method or the Laravel HTML helper methods HTML::script and HTML::style.
i.e to include Javascript file:
- <script src="{{ asset('js/yourfile.js') }}"></script>
- {{ HTML::script('js/yourfile.js') }}
Hope this helps!
Inside the laravel folder you got public folder put css and js files here and access like this
<!doctype html>
<html lang="en">
<head>
<title>{{$title}}</title>
{{HTML::style('css/style.css')}}
</head>
<body>
#include('layouts.nav')
#yield('content')
</body>
</html>

uploading files with mod_rewrite enabled

I have a weird problem. I'm using Uploadify to let users upload their Avatar. My .htaccess file looks like this:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^profile/([^/]*)$ /mysite/profile.php?a=$1 [L]
RewriteRule ^([^\.]+)$ /mysite/$1.php [NC,L]
</IfModule>
When I visit my page like this:
http://localhost/mysite/profile.php?a=avatar the avatar is uploaded fine.
However, when I try this:
http://localhost/mysite/profile/avatar I keep getting a HTTP error and nothing happens.
So I think this has something to do with the Mod_rewrite rules.
Anyone ever had the same problem before?
Thanks in advance.

Resources