htaccess and css files - php

I have the this project:
In the index I check the url with this code:
if( isset( $_GET['url'] ) ) {
if( file_exists( 'classes/layout/'.$_GET['url'].'.php' ) ) {
require_once 'classes/layout/'.$_GET['url'].'.php';
}
}
And in my .htacces this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*?)$ index.php?url=$1&%{QUERY_STRING} [L]
This works great. When is go to example: 127.0.0.1/test/pages/edit the index includes edit.php but in the index i use a css file named: test.css wich make the whole html background red.
If i go to 127.0.0.1/test/ i see the whole background red. But when i go to 127.0.0.1/test/pages/edit it is white. I checked the urls of the css file and i get this:
127.0.0.1/test/ = 127.0.0.1/test/cache/css/test.css
127.0.0.1/test/pages/edit = 127.0.0.1/test/pages/cache/css/test.css
Does someone know how to fix this?

You need to either make the css links absolute (starts with a /test/) or add a base for all your relative links by adding this to your page header:
<base href="/test/">
(the base URI may need to be adjusted, but it looks like you want it to be /test/)
The reason this is happening is because the browser will guess what the URI-base is depending on the URL that's loaded. When you put http://127.0.0.1/test/ in the browser, the browser assumes the URI base is /test/ and all relative links will have that appended to the front. But when you put http://127.0.0.1/test/pages/edit in your browser, it assumes the base is /test/pages/ and thus your relative links get the wrong base appended to the front.

The problem is that u did not give the proper path to the css. Always try to call style/script from base url as below:
Hope this helps.
<?php
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
define("SITEROOT","http://localhost/test/"); // base to your web directory ie,www or htdocs
?>
//call style in this manner
<link href="<?php echo SITEROOT; ?>Sourcefiles/cache/css/test.css" rel="stylesheet" type="text/css" />

Related

Clean URL's using htaccess breaks my css

I'm trying to clean my URL path's so that I don't have any GET parameters and PHP extensions in all of my links. As an example for what I'm trying to achieve:
http://localhost/projectname/?page=dashboard
needs to be:
http://localhost/projectname/dashboard/
And it actually works, I used the following code in my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} dashboard/
RewriteRule dashboard/ http://localhost/projectname/?page=dashboard
However, the page displays itself without any CSS or Javascript. I tried navigating to my Style.css only to find out that it looks exactly like the webpage itself, instead of showing me my CSS rules.
So what am I doing wrong? Please don't mark my question as a duplicate, I've been looking into similar questions but couldn't solve the problem.
You're changing the relative URI when your URL goes from /projectname/ to /projectname/dashboard/.
When that happens, every relative link on the page will have the wrong base added to it. The browser has no idea that the base is actually /projectname/ when all it sees is the location being at /projectname/dashboard/.
Right now, when your css is linked like:
<link rel="stylesheet" href="Content/style.css">
The browser attempts to resolve it by adding the base from the location, and it'll load:
https://localhost/projectname/dashboard/Content/style.css
which doesn't exist because the "dashboard" isn't actually a folder.
You can either add a base to the actual page by including this in the page header:
<base href="/projectname/" />
Or you can try to un-rewrite the dashboard out of the request (a bit trickier and more error prone) by adding this rule
RewriteCond %{REQUEST_URI} ^/projectname/dashboard/(.*\.css)$
RewriteCond %{DOCUMENT_ROOT}/projectname/%1 -f
RewriteCond ^projectname/dashboard/(.*\.css)$ /projectname/$1 [L]
Note that does this may have an unintended impact on your browser's cache.
Use the absolute path to the CSS.
<link rel="stylesheet" href="http://localhost/projectname/assets/style.css">

mod_rewrite RewriteRule and html links

I'm creating a new website and it has some php in it. The site basically would work like this, i have /index.php?page=category_page. The the category would be the category and the page would be the sort-of sub category / actual page. The rewritten rule would look like this: /category/page.
I've got this:
RewriteEngine On
RewriteRule ^category/([^/]*)$ index.php?page=$1 [L]
But i dont know how to separate the category and the page, any help?
The second thing is, in my index.php (for instance) i have some css from an external file:
<link rel="stylesheet" type="text/css" href="style/style.css"/>
This doesn't work with the rewrite rule, because it tries to load /category/page/style/style.css which doesn't exist / doesn't rewrite. How do i make it work? I know a simple fix would be to put /style/style.css and that would load from the root of the website, but i'm currently running the website from a sub directory e.g. example.com/new/index.php so that isn't an option. Any help with this?
Problem 1:
You could define multiple parameters in your regex, e.g. category & page (see below).
With such a broad rewrite rule, you would want to add a condition not to rewrite for stylesheets, images, and other assets, though.
I also modified your pattern to only match letters, digits, hyphens, and underscores, which would prevent the use of non-standard characters in your category or page names.
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|png|gif|jpg)$ [NC]
RewriteRule ^([\w\d\-]+)/([\w\d\-]+)/?$ index.php?category=$1&page=$2 [L]
Problem 2:
You're using a relative url in your href, which is appending the stylesheet's location to the current location defined in your browser (/category/page/).
Even though the server-side is rewriting that URL, the browser is unaware of the rewrite.
If you use an absolute URL instead, your browser will define the URL relative to the BASE url (/).
Try this:
<link rel="stylesheet" type="text/css" href="/style/style.css"/>
Use this RewriteRule
RewriteEngine On
RewriteRule ^(.+)/(.+)/$ index.php?page=$1_$2 [L]

.htacces Appears only html with no CSS or PHP

I've been wondering on the internet on how to work with htacces(really hard to learn it). And when I was lurking in the internet, I found this: http://www.generateit.net/mod-rewrite/
Well, I inserted my url(working on localhost):empresa.com/index.php?p=sub_artigo&id=1&cat=Mercearia
and it gave me this(with all options by default):http://empresa.com/sub_artigo/5/Mercearia.html
And the .htacces code was this:RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&id=$2&cat=$3 [L]
And when I generate the url in php I do
$response2[nome_sub_artigo]
And then, when I click the button, it appears like, only html.
example: http://s14.postimg.org/wr137fx4x/htacces_error.jpg
Any idea what is happening ?
It looks like you are using relative links for your assets (images, javascript, css).
That means that when you look for css/my_stylesheet.css, from the new url, the browser will request a url like http://empresa.com/sub_artigo/5/css/my_stylesheet.css.
The easiest solution is to always use absolute urls for your assets, like /css/my_stylesheet.css, etc.
Your PHP code is outputting relative paths to the style sheets.
Example:
<link rel="stylesheet" type="text/css" href="styles.css">
Given your example input URL, this causes the browser to look for a stylesheet at the following URL:
http://empresa.com/sub_artigo/5/styles.css
This is because the browser doesn't know that the URL has been rewritten - it believes it is viewing a file in a subdirectory that doesn't really exist.
Instead you should use an absolute path, such as:
<link rel="stylesheet" type="text/css" href="/styles.css">
Notice the leading / on the path? This will tell the browser to look from the root of the domain:
http://empresa.com/styles.css
In this way you can still decouple your HTML from the protocol and domain/port (so you aren't tied to http://empresa.com) but the path will always be the same regardless of the URL that was used to reach the referencing page.
Try this code :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /empresa.com/index.php?p=$1&id=$2&cat=$3 [L]

PHP/Htaccess - problem with a loader

I have an htaccess file in the root that redirects every request of a page to a specific file:
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]
Now the redirect is easy in the loader.php
include($_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['url']);
I just include the URL,admin/index.php for example.
If I leave the code as is the loader will include the file and print the correct HTML, but it will not load any CSS or JS scripts. If I put echo "test";
just before the include, the loader will load the CSS file. It's something that is killing me. Do I have to specify something in the HTTP header?
I already tried putting <base url="" /> in the header of index.php with no result,
but another strange thing is that with Chrom if I inspect the page and click on the link I'll see the right CSS.
Update 1
I printed the headers_list();. I noticed one thing - when I print an echo in headers_list, an array shows the content-type, so I tried to add it on my own with the header() function but with no result. Still working on it.
Update 2
I've noticed another thing; if I put a <style></style> tag with some CSS it will work fine, but if I use the <link /> tag it doesn't. This doesn't make any sense.
You have to send the correct MIME headers for css and javascript (and images etc.).
Easiest is to just let apache handle those requests. Put all the CSS, JS and images in a folder named 'assets' or something and change the htaccess to
RewriteCond %{REQUEST_URI} !^/assets/
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]

Mod Rewrite path issues

I have successfully created rewrite rules to handle URLs like
http://example.com/xyz
http://example.com/xyz/
http://example.com/xyz/abc
http://example.com/xyz/abc/
Here is the mod rewrite & regex:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([0-9]+)(?:\/)?$ /index.php?p=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$2 [L]
This works great, but the problem is that when I go to http://example.com/xyz/abc, my script gets the correct input (n=abc) but the paths are messed up, so all relative URLs in my code are broken, as they are now relative to xyz instead of the root.
Is there a way around this at all? As you can see above I have tried to redirect to /index.php to force the path to be correct. My brain is fried after a long day of regex and code, so I hope it's not something disastrously trivial.
Use the HTML base element to force all relative URLs to use a different path than the one the browser displays.
<base href="http://www.example.com/"/>
...
link
The relative URL will use "http://www.example.com" as its base, even if the browser thinks it's looking at the page "http://www.example.com/xyz/". So the link goes to "http://www.example.com/relative/url" instead of "http://www.example.com/xyz/relative/url"
And there's the Stack Overflow way of doing it, in which every URL is either an absolute path (to resources like images) or paths including the root (i.e. "/feeds/questions/123"), which avoid the relative path issues.
The three options are:
1) Use a base tag (this will affect every relative URI on your page, including links, images, scripts, stylesheets and so on)
<base href="http://yoursite/">
2) Change all of your links to fully qualified URIs
<a href="http://yoursite/yourpage.html">
3) Use the "/" prefix to show that the path is relative to the root on each URI.
<a href="/yourpage.html">
I have personally used the base-tag option the most, which does get some bad press (from people that have used it without really understanding it). When it comes to mod_rewrite, the base tag is perfect as you probably DO want all your paths to be relative to the root, including all your images, css, scripts and links.
Edit: I assume you are talking about URLs in your HTML code, e.g. to images and stylesheets, that are broken.
Nope, as far as I know there is no way around it, because the browser sees a path, and requests resources relative to it. It has nothing to do with the server, and there is nothing you can do.
You will either have to resort to a different splitter that is not interpreted as a directory splitter (e.g. underscores), or use absolute paths, or use the <base> tag. I have never used the base tag myself, however, and it is not very well regarded wherever you look. The best thing would probably be to switch to absolute paths.
modify your last RewriteRule to include the path. you have the path in the regular expression.
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ $1/index.php?n=$2 [L]
or alternatively (depending on what you are trying to achieve):
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1/$2 [L]
I tried using the BASE element in my pages as a shortcut instead of changing all urls. Add the base element as follows:
<base href="/">
And here are the results:
This: <link type="text/css" rel="stylesheet" href="my.css">
Becomes: <link type="text/css" rel="stylesheet" href="/my.css">
This: <script type="text/javascript" language="javascript" src="include/my.js"></script>
Becomes: <script type="text/javascript" language="javascript" src="/include/my.js"></script>
This: <a href="foo.html">
Becomes: <a href="/foo.html">
This: <a href="foo/bar.html">
Becomes: <a href="/foo/bar.html">
You can always override the base tag where necessary:
This: <a href="/foo">
Remains: <a href="/foo">
This: <a href="/foo/bar/">
Remains: <a href="/foo/bar/">

Categories