htaccess make alias and rewrite rule - .htaccess

I have html in /html/test-html/index.html here. There are also css and images like this one: /html/test-html/css/style.css
When I open index.html in browser styles and images are loaded correctly.
When I'm trying to render it's contents by php echo function I get problems with 404 error, because paths in index.html and style.css are relative, like this one:
<link src="css/style.css" rel="stylesheet" type="text/css" />
So when I echo index.html contents in my script (i.e. by url like http://localhost/product1) browser tries to find style.css by this url:
http://localhost/css/style.css
But actually file is located in
/html/test-html/css/style.css
I think that good idea is to simulate URL like browser in http://localhost/html/test-html/, but actually we are in http://localhost/product1 (with RewriteRule), but without a redirect.
In other words, I want browser to think that http://localhost/product1 is http://localhost/test-html and server to think that http://localhost/product1 is http://localhost/index.php?r=product/view&id=1
Is it possible?

Related

My html cant find my linked javascript event though its in the correct directory, very stumped here

where I link index.js
<script src="/assets/js/index.js"></script>
Folder Directory is such
assests
-css
--style.css
-js
--index.js
enter image description here
Error Im getting
index.html:25 GET file:///assets/js/index.js net::ERR_FILE_NOT_FOUND
I guess you are using an express app(by the tag in question). The static resources such as your javascript, css, images, etc. should be inside the public folder which gets served on running the application. Could you try moving the assets folder inside the public folder and then have a script tag pointing to that resource.
Maybe it helps to remove the first slash:
<script src="assets/js/index.js"></script>
Or startend with a dot
<script src="./assets/js/index.js"></script>

Connecting to stylesheet that is several directiories above

I am making a Web Project using NodeJS and in which I'm linking to the stylesheet in the header file, which I've included in all of my EJS files
The location of the header file is as follows
root -> views -> partials -> header.ejs
The location of the stylesheet is as follows
root -> public -> style.css
I've used this line to link with the stylesheet in the header.ejs file
<link rel="stylesheet" href="../style.css">
Now, this stylesheet is being rendered in all pages, except for one.
Path of page where the stylesheet isn't working :-
/campgrounds/:id/edit
Other pages (where style.css is working properly) :-
/campgrounds
/campgrounds/new
/campgrounds/:id
What is the reason behind this and how to solve this
Best practice for linking to resources is to use absolute pathing which means start at the root and work up the structure.
By using / it will start at the root of your application directory.
<link rel="stylesheet" href="/public/style.css">

Solving shortening url with htaccess

I have rewritten my url using htaccess .After navigating to the page, the layout distorts and no value fetched from the database is printed out.This is the main URL
http://www.deffsale.com/personalBusiness/PersonalBusiness.php?item=ladys%20Fashion&page=1
and I'm navigating to this page using this one
http://www.deffsale.com/personalBusiness/all/ladys-Fashion/1
This is my ht-access
RewriteEngine On
RewriteRule ^About-us AboutUs.php [NC,L]
RewriteRule ^personal-business personalBusiness [NC,L]
RewriteRule ^personalBusiness/all/([0-9a-zA-Z_-]+)/([0-9]+) personalBusiness/PersonalBusiness.php?item=$1&page=$2 [NC,L]
please help me solve this because it has been a disturbance past three months
You need to include this line just below <head> section of your HTML:
<base href="/personalBusiness/" />
so that every relative URL is resolved from that base URL /personalBusiness/ and not from the current page's URL.
It is a path issue with your CSS and images related to the rewrite rules. Add a rule like this before the other rules:
RewriteRule \.(js|css|jpe?g|png|gif)$ - [L]
which will allow those requests to be passed unmodified.
In addition, make sure the URLs for JavaScript, CSS and image files are coded relative to DocumentRoot. For example if your files are organized like this:
/ - index.php
+ css
+ base.css
+ color.css
+ images
+ logo.png
The style tags would be:
<link rel="stylesheet" type="text/css" href="/css/base.css">
<link rel="stylesheet" type="text/css" href="/css/color.css">
And images would be:
<img src="/images/logo.png" alt="logo">
As the other respondent suggested, you may use a base tag with relative URLs instead. In that case, you would code the tags like:
<base href="/">
<link rel="stylesheet" type="text/css" href="css/base.css">
<link rel="stylesheet" type="text/css" href="css/color.css">
And images would be:
<img src="images/logo.png" alt="logo">
The difference is that the base tag can be used to set the reference point for the relative URLs. Bear in mind the base tag will affect all relative URLs on your page.

website directory structure and file paths

I am attempting to make a small website with a directory structure like this:
rootURL/
landing/
aboutME/
common/
app1name/
app2name/
etc...
Within each of the sub folders are the directories css/ , js/ and html/ so I keep my pages and apps separate and my file types separate within them, which seems sensible to me.
My root page I want to load when some one goes to rootURL.com is
rootURL/landing/html/landing.htm
1) Is this something which can be typically configured on a web server?
2) And is it possible to reference paths to css and script files in different directories? For example my lanidng page rootURL/landing/html/landing.htm , can it use the line
<script type="text/javascript" src="rootURL/common/jQuery.js"></script>
and if not is there anothr way to access files in other directories?
Thanks
All of that is possible. For your landing page, will either have to have a redirect page in the root directory, use URL rewriting, or simply put your home page in the root directory.
To reference paths you can use
<script type="text/javascript" src="/path/to/file.js">
or
<script type="text/javascript" src="../relative/path/to/file.js">
or
<script type="text/javascript" src="http://rootURL.com/path/to/file.js">

rewrite friendly url with .htaccess issue

I want to make Friendly URLs, but when I try my PHP code works correctly but my CSS and JS files are not loaded correctly. It tries to get these files from different path.
I'm using WAMP, and I've created bsp directory for my project.
There are two folders for js and css files and I've used the css and js files in my php pages like
< script type="text/javascript" src="js/menu_config.js">< / script >
< link rel="stylesheet" type="text/css" href="css/menu.css" / >
and the .htaccess file code is here:
RewriteEngine On
RewriteRule ^products/([0-9]+)_([0-9]+).html$ products.php?maid=$1-$2 [L]
But when I browse through url http://localhost/bsp/products/2_7.html,
It does not load the js and css files.. at that time the path of js and css files becomes
http://localhost/bsp/products/css/menu.css etc
the css and js path should be http://localhost/bsp/css/menu.css.
I hope anybody will understand this problem as soon as possible.
This is just to do with your paths when including the files. You're currently using relative links, which means that "js/menu_config.js" will be appended onto the current URL.
Instead, you need an absolute path. By prepending your URLs with a /, you denote that you want to specify the path relative to the document root (rather than the current subdirectory). I'll leave out what the paths should be for now, filling them in is an exercise for you.
This should work
< script type="text/javascript" src="/js/menu_config.js">< / script >
< link rel="stylesheet" type="text/css" href="/css/menu.css" / >

Resources