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]
Related
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">
I'm trying to convert my app links, so that a link like this:
http://localhost/index.php?id=13&category=Uncategorized&title=just-a-link
gets converted to this:
http://localhost/13/Uncategorized/just-a-test
so far I was able to do it using:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
but that completely breaks links to css and other files as it redirects every request with query to index.php
so I changed it slightly so that it only runs when the first query is a number:
RewriteEngine On
RewriteRule ^([0-9]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
this one doesn't break css and js files, however when you go to the link for example http://localhost/13/cat/test then try to go to another link like http://localhost/19/Uncategorized/something by clicking on it inside the previous page it will instead redirect you to something like http://localhost/13/Uncategorized/19/Uncategorized/just-a-test
How do I achieve what I described without any of these weird side effects??
Use :
RewriteEngine On
RewriteRule ^([0-9]+)/([^/.]+)/([^/.]+)/?$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
And add this code to your html <header>:
<base href="/">
OR
<base href="http://www.domain.com/">
To fix relative css and js links
I decided to use mod_rewrite to make my URLs look better. I created very simple rules:
RewriteEngine on
RewriteBase /
RewriteRule ^(profile|contact|help|events|account|logout)/?$ index.php?p=$1 [NC]
RewriteRule ^home/?$ index.php [NC]
RewriteRule ^PlaceOrder/([0-9]+)/?$ index.php?p=mos&gc=$1 [NC]
It almost works well but it doesn't. So for example, [NC] is supposed to make it case-insensitive. However, for some reason, when I type for example localhost/Help in the browser, for some strange reason, it redirects to home page.
Another issue is with the last rule. If I type in localhost/PlaceOrder/1 it works as expected and opens index?p=mos&gc=1. But after that if I click on, for example, Account button, the browser uses this URL: localhost/PlaceOrder/account. Which is wrong. It should only use localhost/account. Why is it adding that sub-directory in there? It is only happening after the last rule is used. Prior to using the last rule, all links work well.
I'm going to guess that the localhost/Help isn't because of the rules and because of something in your index.php script. There's nothing those rules do that could possibly rewrite Help to the home page.
The second issue is a matter of a relative vs absolute URL issue. You're probably using relative links in your pages and because you've changed your relative URL base (which would be /PlaceOrder/ all relative links will have that prepended to it. You need to either change all your links to absolute URLs (they'd start with a /) or add this to your page's header:
<base href="/" />
if request to my site goes like:
my1stdomain.com/request-1/
my1stdomain.com/any-another-request/
it should open web-pages:
my2nddomain.com/folder/request-1.php
my2nddomain.com/folder/any-another-request.php
I put php file in the web-root of my1stdomain.com with name referer.php
and it looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /$ [OR]
RewriteCond %{REQUEST_FILENAME} (html|php)$
RewriteCond /home3/juraj1/public_html/referer.php -f
RewriteRule ^.*$ /referer.php [L]
</IfModule>
and referer.php looks like:
<?
$login=preg_replace('/\//i','',$_SERVER['REQUEST_URI']);
$html=file_get_contents('http://my2nddomain.com/folder/'.$login.'.html');
echo $html;
?>
but it returns only html content without css styles and images
Can you please help me with it?
I assume the HTML files on my2nddomain.com link to the css files/images in a relative way, like <img src="images/picture.jpg" />. But since this image does not exist on my1stdomain.com, the browser doesn't find it.
Solution 1:
Link to all images/css files with an absolute path, like <img src="http://my2nddomain.com/images/picture.jpg" />
Solution 2
Add the <base> tag to the HTML head of all the files on my2nddomain.com like this: <base href="http://my2nddomain.com" />. This can be achieved by changing the HTML on the fly like $html = str_replace('</head>','<base href="http://my2nddomain.com" /></head>',$html);
There are also other solution with rewriting every request to a css file/image on your 1st server to the corresponding file of the 2nd server, but this will be more complicated.
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" />