Url rewriting HTML and CSS paths - php

I have the following .htaccess file in the root of my application:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This basically just convert paths such as index/var=value/var2=values2/ into index?url=index/var=value/var2=values2/ and in index.php they are elaborated to fetch $controller which is the name of the controller called (in this case index) and the $get which is the substitute for $_GET. (In this case it contains: $get['var'] = 'value' e $get['var2'] = 'value2').
Notice: In the root file, where the .htacces file and the index.php are, we have also the application/ folder (which contains controllers, views, and models), the system/ folder which initialize the framework and the public/ folder which should contains every image/javascript/css/video/audio file which can be accessed.
The .htaccess allows me to access directly every existing file both images and css. Which is great. The only problem that occurs is when the path to that image/javascript/css/audio/video file is inside the HTML or CSS files. So that when you call (*1) <img src="public/img/image.jpg"/> or when you call (*2) background-image: url(public/img/image.jpg); it just doesn't work.
That could be "solved" using absolute paths such as http://dominio.com/public/img/image.jpg but it is tricky when it comes to javascript files. And anyway is not a solution at all, but just a way to bypass the problem.
How could I make (*1) and (*2) work?

Have you tried this path?
/public/img/image.jpg
EDIT/ADD
didn't test this, but you can try
RewriteCond %{REQUEST_URI} ^/public [OR]
RewriteCond %{REQUEST_URI} ^/other_folder [OR]
RewriteCond %{REQUEST_URI} ^/more_folders
RewriteRule (.*) /project/$1
OR, have index.php process everything, and the very fist lines are a check to see if the called files exists based on the current path:
//untested code
$url = str_replace('..','',$_GET['url']);
if(file_exists(dirname(__FILE__).'/'.$url)){
//redirect to that file or serve it with PHP
exit(0);
}

background-image: url(public/img/image.jpg);
will look in public/img path relative to the CSS file.
What you should probably be using is ../img/image.jpg
or /public/img/image.jpg

Related

Load Multiple Urls as One Page

Not 100% sure how but I know its simple .htaccess but I have no idea what to do.
Basically I want it to load a index.php file no matter what url they go to in the folder containing the index.php for example:
http://website.com/folder/thisisntafile.php will load: index.php in the folder named folder. This will happen for whatever /folder/file.php is loaded.
Thanks!
Try this :
RewriteEngine on
#Rewrite "folder/file"
#don't rewrite "folder/index.php"
Rewritecond %{REQUEST_URI} !/index.php$
RewriteCond %{REQUEST_URI} ^/([^/]+)/[^.]+(\.php|html)?$
RewriteRule ^ /%1/index.php [NC,L]
This rewrites
http://example.com/foo/bar.php
to
http://example.com/foo/index.php

RedirectRule for Directory

I am having some trouble with Htaccess and RewriteRule.
Basically:
I have a directory called /_dev/WEBDEV and want people to access it from /WEBDEV on the server.
(http://domain.com/WEBDEV will point to http://domain.com/_dev/WEBDEV)
Here is the htaccess code I have used
RewriteRule ^WEBDEV(|/)$ /_dev/WEBDEV/
RewriteRule ^example(|/)$ /_dev/example/
So when i go to http://domain.com/WEBDEV, the page shows, but there is no CSS styling or images.
What i mean:
<img src="img/shape3.png">
the url on the server would be /_dev/WEBDEV/img/shape3.png
but with the Htaccess it gives
http://domain.com/img/shape3.png
as the image (and throws a 404).
Plus, i have a file on my server /_dev/WEBDEV/app1.php and with the htaccess it gives a 404 not found.
First, define your RewriteBase correctly
RewriteEngine On
RewriteBase /
Your rewrite rule should not be triggered if the requested url matches a file or directory.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
And you'll need to add the rule's REGEXP match to the rewritten path
But first, rewrite the path to the WEBDEV directory so it always have a slash after it
RewriteRule ^WEBDEV$ WEBDEV/ [L,QSA]
RewriteRule ^WEBDEV(.*)$ /_dev/WEBDEV$1 [L,QSA]
RewriteRule ^example(.*)$ /_dev/example$1 [L,QSA]
This is quite like creating aliases, but it can be done in .htaccess
For external resources like css and html, you just have to change their paths to
<img src="/img/shape3.png">
for example. This way the absolute path is calculated from the domain name (or base path), resulting in yourdomain.ext/img/shape3.png which should work.

.htaccess specified url with base file

I have a directory site and its sub-folder and files in it.
It also contain a .htacess and below is the code
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?cc=$1
so my below url will is effective and working good.
http://localhost/site/sweetinc
And below code in index.php below
<?php
if (isset($_GET['cc']) and !empty($_GET['cc'])) {
echo $_GET['cc'];
} else {
die("Sorry wrong code");
}
?>
And this is working good.
Now, i want to access or display all files as normally
http://localhost/site/sweetinc/home.php
http://localhost/site/sweetinc/test.php
home.php and test.php are located in site directory. And if it redirects to other sub-folders then it should
be visible like
http://localhost/site/sweetinc/sub-folder1/test3.php
Is this possible, as I am working seperating a group using seperate directory using .htaccess and considering all files in site directory as base files
The trick here (if I understand your question correctly) is that you don't want the rewrite rule to be in effect when you're trying to reach a file that actually exists, right?
In that case, just before your RewriteRule, add these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
That way, the rewriterule only takes effect when the url (/site/sweetinc for example) does not really exist on the server. If it does (/site/sweetinc/home.php) the rewriterule is skipped and the file is shown.
(-f checks if the filename exists, -d checks if the directory exists, so /home/sweetinc/somedir/ should work too)
Update based on updated question
You need two separate rules for this. First of all, if the /sweetinc/ directory in the url is used, refer them to the /site/ folder:
RewriteRule ^sweetinc/(.*)$ /$1 [L]
Then, if the file does not actually exist, let the index.php handle it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cc=$1 [L]
Some examples:
I have the following files in my files subdomain:
/index.php (which has the same content as yours does, so it reads out ?cc=
/circle3.PNG
/the .htaccess with the above rules
http://files.litso.com/sweetinc/hello shows the index.php that echoes "hello"
http://files.litso.com/sweetinc/circle3.PNG shows the actual file in /
(note, I don't have a /sweetinc/ directory, it's all faked)

url rewrite problem css messing up

basically what im trying to do it :
http://mydomain.com/1/1
to
http://mydomain.com/index.php?mid=1&mpid=1
and im using these codes in htaccess.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+) index.php?mid=$1&mpid=2 [NC]
it works fine but the problem is that css get messed,its not loading css and images.
whats the solution ?
The problem is that your CSS is no longer relative to the root of your site.
Your index.php file is at /, but when you rewrite to /1/1, the client thinks you actually are at /1/1, and looks for the CSS file relative to that path, as it should.
What you need to do is reference your CSS at the root, /style/something.css or whatever it is. Just make sure you have / at the front of that path.
Use the base element to set the base URL for all assets (CSS and images, etc). Here's the docs: https://developer.mozilla.org/en/HTML/Element/base
<base href="http://www.yourdomain.com/">
Also, your .htaccess may be redirecting the calls for your CSS and images. Change your .htaccess to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?mid=$1&mpid=2 [NC]
... that will check to make sure the requested file (-f) and the requested directory (-d) don't exist before doing any redirects.
There can be two problems:
CSS and Images are not loading at all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz)$
RewriteRule ^([^/]+)/([^/]+) index.php?mid=$1&mpid=2 [NC]
Or the images now have different path. Always put slash at the beginning of the path, it will help. Especially in CSS file, where the path is taken relative from CSS file, not actual url.
Prepend
RewriteCond %{REQUEST_FILENAME} !-f # Existing File
before your RewriteRule - it prevents existing files, like css files, from being rewritten.
Maybe the sript sends the wrong Content-Type. CSS-Data requires Content-Type: text/css.
PHP:
header("Content-Type: text/css");
With a file extension like '.php' no browser will sniff it correct.

Url rewriting broke link to css

I'm using the following setting for url rewriting:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
In index.php is parse $_GET['url'] so that in the following examples:
ROOT/user/id/1/name/bobby // user is the page, id = 1, name = bobby
ROOT/blog/post/123/title/welcome // blog is the page, post = 123, title = welcome
So that the first parameter(? i don't know how to call it) is the page name then the following couple of parameters are like "keys/value".
Now when i browse ROOT/ the link to stylesheets that are inserted inside the html of the page and the page are shown correctly.
I fi browse ROOT/index (which is the same as ROOT/) it shows the page (with contents and other stuff) correctly but the links (even if in the html structure are correctly written) to stylesheets are not loaded. And i can see that from the fact that my page has no css at all when i load it.
How can I fix this?
EDIT
The css file's path is as follows:
project/view/css/common.css
The file where is it included is in
project/public/index.php // the one with .htaccess and rewrite rules
This brings me to make a link (inside the index.php) such as
../view/css/common.css
But this works different depending on how the url seems. For examples:
# For URL = public/
project/view/css/common.css // good
# For URL = public/index/
project/public/view/css/common.css // broken
# For URL = public/index/key/value
project/public/index/key/view/css/common.css // broken
Comment doesn't let me format the code, can you please try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Update
You can try something like this in the <head> section of your pages to support relative path for your image/css/js files referenced on that page:
<head>
<base href="http://www.example.com/static-files/" />
</head>
I think you have two problems (as the given answers didnĀ“t solve your problem yet...):
You are rewriting all file-names so when the browser is requesting css/index.css your rewrite transforms that to index.php?url=css/index.css. #cOle2's answer should solve that.
You are not using absolute paths for your css files. The server is translating the requested page like /user/id/1/name/bobby to /index.php?etc.... but when the browser requests for example the css file that is something like css/index.css in your code, it will actually request /user/id/1/name/bobby/css/index.css and that file does not exist. You can solve that by using only absolute paths to your external files (css, js, images, etc.) like /css/index.css.
Edit: Based on your comments, both your paths are relative and your css is not accessible by the browser, so you need to:
Move the css to the project/public directory (like project/public/css)
Use absolute paths to your css like /css/index.css.
Make sure the urls to your external files are not rewritten by the server.
You can try removing certain file extensions from the rewrite rule, for example the following rule will disregard images, css and js files.
# Do not process images or CSS files further
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
Edit: This is how I would apply it:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Categories