I use codeigniter framework and I have file sw.js that located in the application root folder.
I'm having trouble accessing it.
http://localhost/project/sw.js
Any help is very appreciated! Thanks
You must put all assets (css, images, js, plugins...) outside application folder. For example create a folder "assets" in your root path. For example usage;
<?=base_url('assets/js/custom.js')?>
And change .htaccess file like this;
RewriteEngine on
RewriteCond $1 !^(index\.php|assets)
RewriteRule ^(.*)$ /index.php/$1 [L]
Related
I'm trying to deploy my Laravel app and block the access to the others files like .env
I put all my laravel app in the www folder, and I add this htaccess :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But when I go to my domain url I have all the files.. seem like my htaccess is not working (he's on the Laravel app root)
Here's a simple method using only a .htaccess file placed in Laravel's root directory - e.g. alongside app, bootstrap, config, ... No changes whatsoever are necessary to your code.
The file rewrites all the requests so that requesting /file.png would in fact return /public/file.png and anything else is routed to /public/index.php. This also ensures that nothing outside the public folder can be accessed, thereby protecting any sensitive files like .env or database/*.
The simple method
This method assumes that DOCUMENT_ROOT is set properly by your Apache server. Try this first and use the 2nd method only if it doesn't work on your server.
.htaccess
RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]
The slightly more complicated method
If your server doesn't set DOCUMENT_ROOT properly, then you'll need to use an absolute path in RewriteCond. That is, an absolute path on the server's filesystem. You can get it by copying the following script to the directory where your Laravel installation will reside and visiting its URL - i.e. http://example.com/get_doc_root.php.
get_doc_root.php
<?php
echo getcwd();
This should result in something like /var/www/example.com/web. Use the following .htaccess file and replace [[your path]] with the actual path you got.
.htaccess
RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond [[your path]]/public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]
In our example case, the RewriteCond line would look like this:
RewriteCond /var/www/example.com/web/public%{REQUEST_URI} -f
I am new to mod_rewrite. I'd like to redirect all htm files in Wordpress root directory to docs/filename.htm on localhost Wordpress installation meaning localhost/a.htm should go to localhost/docs/a.htm. How can I achieve that? I am using /web as the base dir of Wordpress. I tried this without any success.
RewriteEngine On
RewriteBase /web/
RewriteRule ^([^/]*?).htm$ docs/$1.htm
Also is it possible to redirect only from the root folder? - i.e. localhost/a.htm will redirect to localhost/docs/a.htm but the rule should not apply to localhost/otherdir/a.htm
Any help would be appreciated.
RewriteEngine On
RewriteRule ^(.*).htm$ docs/$1.htm [L,NC]
Try this
My question as in the title
I want to keep the assets folder which contain (css, js, images) inside the application folder not outside.
If I keep the assets folder inside the application folder, I can't access to the files that inside it.
.htaccess file code
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
The standard Application structure:
Application
app
assets
sys
.htaccess
index.php
The previous structure has no errors.
I want the structure to be like the following:
Application
app
assets
config
controllers
......
sys
.htaccess
index.php
How can I do that ?
Your problem is that the .htaccess file you're using is rewriting the URL of your static assets, breaking the links, so you need to change it.
If you keep your assets/ folder inside the root of the application and amend your .htaccess to something like this, that should solve the issue:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
The change is on the second line - essentially the condition is "If the route doesn't start with index.php, assets, or robots.txt, then rewrite it to include index.php at the start". The addition of assets should solve the issue.
EDIT: By default, CodeIgniter has an .htaccess file inside the application/ folder that denies access to anything inside it. If you really, really want to be able to serve your static files from that folder, you can amend that file. However, it would be a really bad idea. Your static assets should live in the web root, and trying to serve them from the application folder will be fraught with problems - if you allow access to the application/ directory from outside, it could cause some serious security issues.
im using shared hosting. i build my web application with codeigniter and my new web directory will be as follows:
/home/projName/public_html/proj_v3/
under this dir, there are index.php, .htaccess. as shown above, i separated different project version in different directories (ie: proj_v1,proj_v2, proj_v3). the web applications allows photo upload and it's stored in /home/projName/public_html/proj_v3/application/uploads. proj_v2 currently has the latest uploaded photo since it's still in use. because i have different version of projects, i want to place directory uploads in a common directory such as /home/projName/public_html. so that for any new version of projects, i won't have to move the upload folder to the new project folder. this might cause downtime.
currently, the .htaccess file in /home/projName/public_html/proj_v2 is:
Options -indexes
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1/ [L]
i have another .htaccess in /home/projName/public_html/ is:
Options All -Indexes
RewriteEngine On
Rewriterule ^(.*)\.*$ proj_v3/index.php/$1/
RewriteOptions MaxRedirects=3
basically how should i go about editing the 2nd .htaccess file in /home/projName/public_html/ so that if the web directory is domain.com/uploads/image.png, it gets the image from /home/projName/public_html/uploads. currently, the 2nd .htaccess file tells domain.com to point /home/projName/public_html/proj_v3
RewriteRule \.(jpe?g|gif|png|wmv|bmp)$ /uploads/$1
Might work for you. You can add other formats as needed.
What you want to do is allow the paths to real files and folders to exist, while redirecting everything else to CodeIgniter, and you can achieve this using this in your second .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Definitely worth reading this article on the CodeIgniter website about mod rewrite
I actually gave a similar answer before: mod_rewrite in Users/<username>/Sites directory on OSX
I need to route for example to "www.url.com/controller/function" i put the code in 'config.php' in routes but, doesn't work. i need to load to views in the same controller, without put 'index.php' in the url. It is possible?
By default, the index.php file will be included in your URLs:
www.url.com/index.php/controller/function
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
Put the .htaccess file containing the above code to your app root folder.
here is the helping url to remove the index.php from url
User Guide remove index.php
you have to use .htaccess file to remove index.php from url
Hope it helps
Updated
Have a look in the application\config\config.php file, there is a variable named 'index_page'
It should look like this
$config['index_page'] = "index.php";
change it to
$config['index_page'] = "";
here is the .htaccess file you can use this..
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Here are some use full links.. you can follow these..
remove index.php stackoverflow
remove index.php codeigniter forum
Enable mod rewrite from your server
Hope these will help you