i have .htaccess file and this is a script within it:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^register/$ index.php?c=Page&a=register [L]
</IfModule>
in my layout file, i load css and js with absolute path like this:
<link href="/public/assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="/public/assets/bower_components/bootstrap-extension/css/bootstrap-extension.css" rel="stylesheet">
and i access localhost/project/register/ but
the css/js file is not loaded bacause of error:
GET http://localhost/public/assets/bootstrap/dist/css/bootstrap.min.css net::ERR_ABORTED
why it directly access to /public after localhost? it should be localhost/project/public not localhost/public
if i remove first / at css link it access to localhost/project/register/public
Your css link is not correct. It is starting with /public which means Apache will attempt to find it in the public folder under site root.
You should update your path to relative one:
<link href="public/assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="public/assets/bower_components/bootstrap-extension/css/bootstrap-extension.css" rel="stylesheet">
And then add this just below <head> section of your page's HTML:
<base href="/project/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
Alternative to <base ../> tag in HTML, you can use this redirect rule in your .htaccess as very first rule:
RewriteRule ^public/.+\.(?:css|js)$ /project%{REQUEST_URI} [L,NC,NE,R=301]
However do note that a redirect rule (even with R=301) will still make a new client to send 2 requests to your web server. First with /public/... URI and 2nd a /project/public/... URI after redirect.
It seems that you have two separate questions.
For your question, there is nothing in that .htaccess rule you've shown which would change the way that an existing .css or .js file would be displayed. And so, very likely either your links weren't working before, or you had some other content in the file before to rewrite the URLs differently that you have removed. That file is simply stating that if a URL is given and there is no file or directory which matches it, then rewrite the URL for a certain URL. That does seem to be a mistake, since the URL either exists or does not.
Your second question is about the URLs and why you don't have /project in them. I'm not certain why you believe they should have /project in them, but I'm assuming that you had assumed that because your original HTML is in that directory. You are starting your link with a /, as in /public/assets/bootstrap/dist/css/bootstrap.min.css". That / means that the path is absolute, rather than relative, meaning that the path is started directly after the hostname. If you remove the initial /, then the path would be relative to the URL that it is called from.
If you want to load these files relative to the domain root and the public directory is not in the domain root, you'll need to provide the full domain relative path:
<link href="/project/public/assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="/project/public/assets/bower_components/bootstrap-extension/css/bootstrap-extension.css" rel="stylesheet">
Related
For an instance, primary URL is http://example.com running on apache server.
I want to put my project in subdirectories as root>projects>thekingfisher resulting into
http://example.com/projects/thekingfisher/
With htaccess, I could access the project
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projects/thekingfisher/
</Ifmodule>
But, some of the files have absolute paths, which are not accessible as in below file
/root/projects/thekingfisher/index.php
<html>
<head>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Test Page</h1>
About
</body>
</html>
As I run my project http://example.com/projects/thekingfisher/
Expected Behaviour:
I want to load the file "css/styles.css" from root/projects/thekingfisher/
Also, "About" link should navigate to "http://example.com/projects/thekingfisher/about.php"
Actual Scenario:
style.css file is being searched at root/css/ to be loaded.
"About" link navigates to "http://example.com/about.php"
Actually, project runs smooth on root. But not properly loading in the subdirectory.
There are many hyperlinks in such pattern, and gets 404 error when tried.
Since this is active project, I don't want to make edits in the source files for modifying the path like prepending baseurl, etc.
You just need a rule in site root to rewrite all requests to /projects/thekingfisher/:
RewriteEngine On
# if file exists in /projects/thekingfisher/ then rewrite
RewriteCond %{DOCUMENT_ROOT}/projects/thekingfisher/$0 -f
RewriteBase .* projects/thekingfisher/$0 [L]
Here comes a question because my current solution is not really satisfying me for the following problem:
I have an own PHP Framework with MVC pattern in development. And my Router works perfect and all but I have one question I could not find a solution for.
Well I route every incoming request to index.php file which is located in the base-path of my framework. Ofcourse it is no problem to work with relative paths when including css such as:
<link rel="stylesheet" type="text/css" href="style/include/css/style.css" />
This works perfect in the browser. Ofcourse it does not matter what I enter in the URL because every request gets redirected as stated above to the index to make a reasonable routing possible.
However when my url contains multiple slashes which look like subfolders, for example: "/manual/details/1_2" then I get a normal routing process but the browser cannot find the css file unless I add "../" for each "/" in my url to map backwards to my base-path.
For example for the above URL this would work instead:
<link rel="stylesheet" type="text/css" href="../../style/include/css/style.css" />
My curent solution:
I wrote a PHP function in my Routing class that determines the required amount of "../" pattern and I always cast the function before implementing a resource to build the exact
path at any time.
Example:
<link rel="stylesheet" type="text/css" href="<?=Router::getInstance()->getSubdirectoryPrefix()?>style/include/css/style.css" />
Needless to say that this is very unhandy and also sucks if you forget to place that function. If your route ever changes or you forget that you are in a subdirectory you will wonder why your resource could not be found.
I also know about putting an alias such as Alias /public style/ in my Virtual-Host configuration of Apache but I want to find another - project and PHP internal way without having an unhandy crap such as pasting the alias function all the time and without setting up a virtual-host option so the framework can stay lightweight and does not require any nasty external options like modifying Virtual-Host.
I'd love to hear your solutions, best would be .htacces - oh and by talking over it I leave my .htaccess code here aswell:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]
Thanks in advance for your help~
You must use absolute paths instead of relative paths for your html links (css, javascript, images, etc).
For example:
<link rel="stylesheet" type="text/css" href="/style/include/css/style.css" />
The leading slash (before style) means to begin from document root folder, then go into style folder, and so on... (you may have another prefixed directory if it's not in root folder)
You had some problems because some of your rules can create virtual directories (e.g: http://domain.com/some/directory/subdirectory/etc/).
Also, in your htaccess, it wouldn't hurt to use a leading slash (or a RewriteBase)
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.php [L]
or (both are the same)
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]
This is my first time using a php framework and I haven't found an effective solution.
I've made my page load style tag correctly using base_url() in an asset_helper file, and it seems to work fine.
generated code:
<link rel="stylesheet" type="text/css" href="http://localhost/p_maricarmen/assets/css/style.css" media="screen" />
but it doesn't load the css file.
I have the same problem with others files like loading images with img tag.
I also have a .htaccess file in root directory with code:
RewriteEngine on
RewriteCond $1 !^(index.php|public|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
for the friendly url.
For more information, if put the style url in my navigator, it generate an html page with 404 error, page not founded.
Modify your .htaccess file with the below code.
RewriteEngine on
RewriteCond $1 !^(index.php|public|assets|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
The reason why your css file is not loading is that you've to specify the Rewrite Condition for the folder where your css and js files have been placed.
You can do the same thing for your images folder.
Simply specify the images parent folder name as i've specified for the assets, and it should work as expected.
Hope this helps you...
Hello I've been running into a few issues with CI lately. When I want to link a css file to pretty much anything, I need to put it directly into the root folder. If I move the file somewhere further in the hierarchy it won't load it. Here's an example of when I am trying to link this core_css.css. It loads the one in the htdocs folder, but not the one in the css folder.
Works:
<link href="<?php echo base_url(); ?>core_css.css" type="text/css" rel="stylesheet" />
Does NOT work:
<link href="<?php echo base_url(); ?>application/OBS/css/core_css.css" type="text/css" rel="stylesheet" />
I've tried putting the css filed in all the other subfolders and linking those, but it only loads the one in the root.
My second problem is that every time I try to test a controller, I need to access it through index.php like this:
http://localhost:8888/index.php/test_controller
Is there a way to get rid of the need to put the index.php in the URL?
For your first problem:
Add .htaccess file in your root web directory and write allow from all it means, in this folder your all files and all folder will not give error Access forbidden! use it like this:
<link href="<?php echo base_url(); ?>application/public/css/style.css" rel="stylesheet" type="text/css" />
N.B:
I usually put all my files like that into an "assets" folder in the application root, and then I make sure to use an Asset_Helper to point to those files for me. This is what CodeIgniter suggests.
For your second problem:
If you are using Apache place a .htaccess file in your root web directory containing the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
for better understand you can see codeigniter URL section through this link
http://ellislab.com/codeigniter%20/user-guide/general/urls.html
and another one is http://snipplr.com/view/5966/codeigniter-htaccess/
For the second question: put the following in an .htaccess file inside your CodeIgniter root folder (htdocs in your case):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
Make sure to set $config['index_page'] to an empty string, and also set $config['base_url'] accordingly (these two options are inside /application/config/config.php)
For the first question: try to make a folder called assets, or something similar and put it in the root of the site (again inside htdocs). Move your css file there, and try to access it by visiting http://localhost:8888/assets/core_css.css.
Also, in case you are using Linux box, make sure the folders you want to access have read permissions.
Background
Trying to recreate StackOverflow-style pretty URLs using Apache's mod rewrite and PHP.
Problem
This works great:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ([0-9]+)$ index.php?id=$1 [L]
</IfModule>
URLs like http://localhost/home/script/5 redirect to http://localhost/home/script/index.php?r=5. The CSS content loads flawlessly.
URLs like http://24.68.226.186/recipes/recipe/5/seo-text redirect, but the relative path for the CSS files is then incorrect:
<link rel="StyleSheet" hreF="css/theme.css" type="text/css" media="screen,print" id="css-theme" />
Directories
The directory structure for the script (index.php) resembles:
htdocs/home/script
htdocs/home/script/.htaccess
htdocs/home/script/index.php
htdocs/home/script/css
htdocs/home/script/images
Question
How do you use mod rewrite to tell Apache to use the "script" directory when serving files instead of any sub-directory appended to the URL?
In other words, how would you use mod rewrite to lop off the "seo-text" part of the URL and (1) ensure it reflects the actual title [sourced from a database]; (2) redirect the browser to the new URL [if necessary]; and (3) tell Apache to ignore all paths beyond the ID in the URL?
I would rather not use absolute paths in the CSS.
Thank you!
Used Logan's advice:
<base href="/home/script/" target="_self" />
This does not work with IE8, due to a bug.