Missing CSS file and images after URL rewrite - php

I'm trying to make user friendly URL using mode rewrite.
My problem is, that after giving category like 'name' to my URL, when I call the page using new URL, it can't load the CSS file or images.
I have a link like:
localhost/mywebsite/project?id=22
New link is something like
localhost/mywebsite/project/22/myproject.project
htaccess code:
RewriteRule ^project/([0-9]*)/.*\.project$ /project.php?project=$1 [L]
(it might not be 100% right but I don't have access to my code right now so I just wrote this and it works fine on the original source)
My root directory is localhost/mywebsite/
and my CSS file is in css/style.css
localhost/mywebsite/css/style.css
my htaccess
localhost/mywebsite/.htaccess
and my project.php file is in
localhost/mywebsite/project.php
So in the project page I have access to CSS file by using relative path,
<link href="css/style.css" rel="stylesheet" type="text/css" />
but when I use rewritten URL page can't find the CSS file.
I can't use absolute path with domain name because I don't have domain yet! and it can be anything.
one way is to use relative path to domain as suggested on the similar questions
localhost/mywebsite/project.php
and when i run my script localy my root directory is
localhost
so css link should look like
href="mywebsite/css/style.css"
but when i go live i should change all links to probably something like
href="/css/style.css"
this seems like lots of work

For your local version add
<base href="//localhost/mywebsite" />
to the head section
and for your live versions change it to
<base href="//your.domain.here" />
reference at http://www.w3.org/TR/html4/struct/links.html#h-12.4

you have to define the base path or the server view path in the connection.php and whenever u want that path, make that global. then that variable will b called and the css or images will take the whole path.
for example
$SVP="http://www.example.com/"
global $SVP;
echo $SVP;
so

Insert an image into the same file with the same relative path as the css href link, load the page in a browser, right-click the image in internet explorer, click properties and you should see where the relative path actually points to.

Related

How to link to a php file that is in a folder named php

I have made my first website and I have a simple contact form that runs a php scripts to send the details inputted in the form to my email. I initially had all the files in the root directory but I am trying to tidy it up so I now have,
all html files in the root directory, I then have folders for php, css, assets, js.
I know that to link to these I need to just do something like,
<link rel="stylesheet" href="css/contactus.css"/>
I believe this tells the html to look in the root directory, then in css folder and for the file contactus.css.
This seems to work for all the css files but not for the php file ( I get a 404 error). I know this file is correct as when all the files where in the root folder it worked fine.
I currently have,
<form action="php/contactform.php" method="post">
I believe my issue is that when using href it will automatically start in the root folder but as I am not using href in this case it is not starting in the root folder. So I just need to find out what I use to link to a different folder to that of the html file (If I put the php file back in the root folder it works fine.
DIR structure
The action and href attributes work exactly same. The final url depends on how you've written the url in these parameters and what is your current url.
Let's say the current url is http://localhost/my-project/html/my-form.html
If your url in action looks like php/contactform.php this is called path relative url. To get final url the browser will append relative url after last / in current url resulting in http://localhost/my-project/html/php/contactform.php.
You can use .. in the relative url if you want to reference directory higher in the structure. Url ../php/contactform.php will result in http://localhost/my-project/php/contactform.php
If your url in action looks like this /php/contactform.php it is root relative url. The main difference is the starting /. In this case the browser will put your url right after the domain name resulting in http://localhost/php/conactform.php.
Based on your screenshot both /php/contactform.php and php/contactform.php should work fine because the url of the html file should be http://your-domain.example/DJ_contactme.html so both urls should result in http://your-domain.example/php/contactform.php
If you are using mod_userdir your url probably looks like http://your-domain.example/~dannyjeb/DJ_contactme.html in that case the php/contactform.php should work as it would result in http://your-domain.example/~dannyjeb/php/contactform.php
From DJ_Contactme.html you target css file like this:
<link rel="stylesheet" href="./css/contactus.css"/>

How to make Dynamic css path?

I'm using not_found.php file for unfound files on server , and it's in Main Directory
it's style is in 'assests/css/main.css' from same Directory ,
so I make href of linking stylesheet
href="assests/css/main.css"
it works perfect if user types 'Main/wrongFile' for example , but if user typed 'Main/ez/wrongFile' , stylesheet is included wrongly due to changing of path
I'm searching for function like
$Path = FindPath('Main/assests/css/main.css');
so it gets that path wherever file is in
then I can simply
href="<?php echo $Path; ?>"
The best way to fix it is using the code below;
<link rel="stylesheet" href="/assests/css/main.css" type="text/css"/>
adding / at beginning of assests/css/main.css simply tells browsers which root directory to look at.
Option 2) Using htaccess. Add the code below into your .htaccess file
RewriteRule ^([^/]*)/assests/css/main.css$ assests/css/main.css [R=301,L,NC]

PHP define() function as URL

I'm developing a website for my conclusion work at school. I'm using XAMPP v3.2.1. to localhost the site.
My site's folder's are configured just like this in htdocs folder:
ibnm
css
js
img
...
site
about
midia
...
includes.php
index.php (HOME PAGE)
My problem starts here: on index.php I'm including includes.php, that's a simple file with define() functions to the folders of my site so I can print the constant on the HTML tags of the site as URL (just like below)
//includes.php
<?php
define("css", "localhost/ibnm/css");
?>
//index.php
<?php
include_once("includes.php");
?>
<link href="<?= css; ?>/bootstrap.css" rel="stylesheet">
But when I do this, the CSS don't function. When I see an <a> tag with the previously defined URL on page it looks like
localhost/ibnm/site/localhost/ibnm/css
instead of
localhost/ibnm/css
It's confunsing 'cause if the <a> tag doesn't have any value (href="") it output localhost/ibnm/site/.
What can be wrong? XAMPP or coding?
Any url is not starts with http then browser will assume that its relative path so it will append to your current path, thats why your getting localhost/ibnm/site/localhost/ibnm/css.
And one small correction in your code, its not good idea to hard code server name in the code, better to get server name dynamically. So that you no need to change while deploying your site in real server.
//includes.php
<?php
define("css", $host='http://'.$_SERVER['SERVER_NAME'].'/ibnm/css');
?>
This is because the browser think localhost is a folder, and then do this ontop of the current path, to fix is just add http:// before the localhost
define("css", "http://localhost/ibnm/css");
Instead of using absolute path you can just add one slash before CSS path -
<link href="/<?= css; ?>/bootstrap.css" rel="stylesheet">

CSS not displayed with $_Get URL

I'm working on displaying a user's profile using $_Get['id'] from the URL.
At this moment I'm able to find the user in the database but the CSS is not being displayed.
Here's the css working:
http://classbrief.com/profile2.php
and here's the css not working:
http://classbrief.com/profile2.php/?id=1
I'm so lost, please help.
Thanks,
Monte
The problem is, you use relative urls to reference your resources (without a leading slash which tells the browser to search for that resource in the current directory).
You can fix the problem by not changing that "current directory" and staying in the home directory of the page.
In the case of your example, the format of the url suggests to the browser that you have a directory called /profile2.php/ and the relative urls in your html tell the browser to look for /profile2.php/style.css.
However, if you want to make a page, with the same relative url references in a sub directory for example at http://classbrief.com/sub/index.php writing
<link href="style.css" rel="stylesheet" type="text/css" media="screen">`
won't work as long as you don't have a http://classbrief.com/sub/style.css file.
So.. in conclusion:
to access a resource in the main / home directory
(http://classbrief.com/) from any sub directory, use a leading slash (/style.css)
to access a resource in the current directory (http://classbrief.com/something/) don't use a leading slash (style.css)
to access a resource in an arbitrary directory (http://classbrief.com/something/arbitrary/) from any sub directory, use a leading slash and write the path to the directory (/something/arbitrary/style.css)
I hope this can be understood

Subdomain en relative URL's

I am working on a webapplication that's running on a subdomain. In the code I used relative URL's all over the place. Nothing special, just the normal way to go.
I have just uploaded the site, but I can't find any file that I want to include. This, for example, is working now:
Site URL: sub.domain.com
CSS files are in: sub.domain.com/css/
Adding the following to the index.php **is working:**
<link rel="stylesheet" href="/css/styleSomething.css">
But including PHP files it the thing that is not working
Site URL: sub.domain.com
PHP Include files are in: sub.domain.com/inc/
Adding the following to the index.php is **not** working:
require_one(/inc/config.php)
If I change the URL's to start with a ./ or with no slash at all it will find the files for the homepage. But that's not going to work when the visitor navigets to a different page.
Am I missing some here of is this a problem with the hosting?
You can use the <base> tag to set the root URL for your site: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
The following should work for your example:
<base href="http://www.blah.com/yadda1/yadda2">
<link rel="stylesheet" href="css/styleSomething.css">
This will reference the stylesheet at http://www.blah.com/yadda1/yadda2/css/styleSomething.css.
If you are using PHP and using several URLs, place your base URL in a variable:
<base href="<?php echo $base_url; ?>">
If I understand correctly, you have the pages under /rel1 under the root of the server.
If so, try using /rel1/css/style.css.

Categories