Displaying a short URL for a website - html

I am not clear on how to frame my question. I am making the website for my college fest, and although I know HTML, CSS, I don't know anything of PHP. I just want to display my URLs like this:-
www.somesite.com/somepage (something like that)
instead of :-
www.somesite.com/somepage/index.html
How do I do that? Thanks for any help. I am new to all this
edit: I have tried something like this, just removed the constraint-
<script>
setTimeout (function () {
if (typeof history.pushState === "function") {
var width = window.innerWidth || screen.width;
if (width < 768) {
history.pushState(null, null, "/short-url");
}
}
}, 10 );
</script>
Didn't work. The console gave an error "Failed to load resource" for a lot of files.

What you are looking for is called URL Rewriting. If you have an access to the .htaccess file
you can define some rewrite rules in it, for example:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^www.somesite.com/somepage/?$ www.somesite.com/somepage/index.html [NC,L]
The first part is the address the user will enter and the second part is the actual address he will be referring to, the third part is an array that contains some flags regarding the rewriting.
NC - the rule should be case-insensitive
L - don't process any more rules if this one is used
Note that you don't have to use the full URL, for example:
RewriteRule ^blog/first-post/?$ /blog/posts/myFirstPost.html [NC,L]
Here's a nice beginners guide.

The answer depends on a particular web server configuration. Usually request url corresponds to a particular directory path relative to site root directory. Some web servers allow to omit such resource names as index.hml, index.php in request url. They will associate implicitly /some/path with /some/path/index.html.
See link

You will want to have a directory structure similar to this:
website/
├── blog
│   └── index.php
├── contact
│   └── index.php
├── index.php
├── projects
│   └── index.php
└── resume
└── index.php
where within each folder (these being the different sections of your website), you have the .php or .html files being named index.extension. This will give you the desired effect.

A really easy and silly way would be to encompass the url you want in an anchor tag and have the anchor tag point to the url you want. for example:
www.somewebsite.com/webpage
Also, depending on the server's OS (I know this is true for apache because it's what I learned on), you don't need to explicitly indicate index.html, it'll load the index of the directory automatically. In some cases, if you exclude the trailing / as you did with www.somesite.com/somepage then it'll redirect to www.somesite.com/somepage/. This happened with my school's website and I don't think they've fixed it.

Related

How does the browser resolve the relative location of './'?

I am combining many small semi-static, single-page webapps into one larger web site. The backend is a lot of proxies, but the forward facing server basically just make it look like the app was moved from the root filepath to a more specifics one. IE:
/
├── css
│   └── app1.css
├── index.html
└── js
└── app1.js
would be moved to
/apps/app1/
├── css
│   └── app1.css
├── index.html
└── js
└── app1.js
This migration has been relatively painless mainly due to the use of ./ in the apps' html files, such that most apps just load their resources relative to their new location. The problem I am having is that some apps are resolving ./ differently. For these trouble cases, the primary html file gets loaded; however, the ./ in the script and style elements are resolving to a higher file-path (IE: I would expect ./ to resolve to /apps/app1 but am getting /apps). It may be a coincidence, but the troubled apps often have additional, non-index HTML files.
What are the rules for how ./ is resolved?
Determine the base URL
This is usually the URL of the HTML document
It might be overridden by the base element
For CSS it is the URL of the stylesheet
JS is always with respect to the HTML document
Remove everything after the last / in the path section of the URL
e.g. the base URL for https://example.com/example/foo?bar=baz#fragment is https://example.com/example/
Keep in mind that an HTML document might be visible at the path /example and /example/ and you should avoid this by making one path canonical (I prefer the one that ends in a /) and redirecting to it from the other
Strip the ./ from the front of the relative path
Append the result of step 3 to the result of step 2
A common gotcha is to confuse URLs with file paths. While a simple static site will usually have a direct 1:1 mapping between them, many modern sites will use routing code (e.g. for Express for HTML documents and a separate static route for static files like images, js and css.

CSS background-image not loading

I am having an issue getting my background-image to load as a live website (using github pages). When i load it locally I can get it to work by entering in the full directory. However when I then move it to Github the directory changes and so it no longer works. To summarise, when I use background-image url("images/picture.jpg"); it will not work at all. I have to type in background-image url("c/onedrive/webroot/images/picture.jpg"); so the whole file name. However that does not work when put onto Github. Any help would be much appreciated. (:
As others mentioned, it seems like a file path issue. When using relative paths like images/picture.jpg, make sure the target image is in the correct relative location as indicated. If your images folder is located at the project root, your background-image url had safer/better be /images/picture.jpg with the / at the beginning to denote the project root.
Update
Looking at your code on the repo, I fixed it by updating the relative path — background-image: url("../images/abstract.jpg");
Your folder structure looks like...
/
|
├── css/
├── images/
├── js/
├── objects/
└── video/
Since your stylesheet in css folder is pointing to a file in images folder, you'd need to use a correct relative path.
I have faced this problem before. If you're background-image doesn't work check if your path is correct you could also right click the image in the file browser open Properties
--> Security and copy the path from there. If this doesn't work copy the image address directly from the browser and paste it there.
Check your path is correct
body {
background-image: url("path_of_image_from_this_page.jpg");
}

Style Sheets and hiding .html extension

I'm not amazing with web stuff but I have a small portfolio site which I am redesigning. I am looking to hide the example.com/page.html and make it website.com/page.
I added the rewrite engine which I found on here for the .htaccess
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.html
The only problem is that when I use the RewriteEngine, the original path of /page.html loads as normal, but when i test it as /page/ it seems to lose the style sheet, I've tried relinking the style sheet as .../style.css instead of just style.css, but still all the images and style sheet seem to go missing.
It's probably something pretty damn simple but I need some help here.
what i understood from your words is that you want to make your url like :
http://www.example.com/page
ok . if you don't have to use htaccess , you can do this to do what you want .
just change the file name (page.html) to (index.html) . now create a new folder in your root and move the index.html to there !
and if the file , (page.html) is your home page , just change its name into index.html and leave it alone :D !
Your problem doesn't necessarily have to do with the RewriteRules, but with paths. You should, in general, always use absolute paths to images and assets. So instead of loading style.css or ../style.css, load /style.css or /static/styles/style.css. Otherwise the browser tries to resolve relative to the page location, and /page is considered to be in the root folder while /page/ is thought to be its own folder.
Iow, if you load style.css from your page:
From /page it will load /style.css
From /page/ it will load /page/style.css
Always use absolute paths, save yourself the pain and frustration.
Apart from that, ensure the images and assets are also not rewritten into .html extensions as mentioned in the other answers.
You probable don't want to rewrite every possible extension, so you might want to try something like this:
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
What this should do is check to see if a file exists with an .html extension. If it does it should transparently append .html to the path prior to doing the actual "lookup". So, if you make an HTTP request for
http://yourdomain.tld/somepage
and your site has an HTML page called somepage.html, the actual URL that gets processed will actually be
http://yourdomain.tld/somepage.html
EDIT:
I'm including a Dropbox link for a self-contained example that shows the suggestion above works: https://www.dropbox.com/s/6md9gviv0r2rf9v/xampp.7z
It contains a portable version of Xampp + the source files from this rather nice tutorial: http://coding.smashingmagazine.com/2009/08/04/designing-a-html-5-layout-from-scratch/
Unpack the xampp.7z file somewhere (I recommend the Desktop) and then find and execute the setup_xampp.bat file. It will adjust all the internal paths to your local filesystem. Then, run xampp-control and start Apache. Once that's running, navigate to http://localhost:8080/testsite/ - this is the test site. You should be able to bounce back and forth from http://localhost:8080/testsite/ to http://localhost:8080/testsite/contact - both pages have a .html extension.
HTH.

How does Apache know when and how to look for relative paths?

If we have a file structure like so:
/
├── aboutus
│   ├── aboutus.html
│   └── logo.jpg
├── validate.php
├── index.html
└── logo.jpg
Although one would have to specify the folder when linking to aboutus.html ( ie. <a href="aboutus/aboutus.html">), in both index.html and aboutus.html if you place logo.jpg, Apache will know which one to load. It probably looks for the file in the current directory only. I have the following directive in my Virtual Host section:
RewriteCond %{REQUEST_URI} !^/validate.php
RewriteRule ^/(.*) /validate.php?uri=$1
I am a total newbie with these rewerite rules, so this is what I want the above to do: I want it to intercept any requests from the browser. These include requests entered in the address bar, as well as requests from within web pages (ie. js files, images...). Next I want to send the requested file to validate.php
validate.php obviously validates the user, but essentially just returns whatever file the user requested. If aboutus.html inserts its picture like so <img src = "logo.jpg"/>, how can I ensure that validate.php returns /aboutus/logo.jpg and not /logo.jpg?
Essentially, the absolute path is determined from the base resource URL and a relative resource URL (more on this here). For your validate.php to be able to properly resolve the relative URLs for images, stylesheets, etc., you should somehow let it know the absolute URL of the document containing these resources (and this is even trickier if the document has the <base> tag).
Puk, You have to keep two aspect separate:
Any HTTP request, such as a GET are absolute, and the browser will convert any relative href or src attributes based on either the path of the referring document (or the path defined in a <base> tag) so if you load http://somehost/fred/xx.html which then references an image style1/images/pic.png then the browser will request http://somehost/fred/style1/images/pic.png
How mod_rewrite processes paths is quite different. That's the business of the server. See the RewriteRule Documentation which explains how relative and absolute paths are evaluated inside a per-server configuration (apache.conf or vhost.conf) and inside a Per-directory configuration (e.g. in an .htaccess or block)
Anyways in htaccess file you always work with absolute path. I mean starting with root slash.
try to use:
RewriteCond %{REQUEST_URI} !^/validate.php
RewriteRule ^(.*) /validate.php?uri=$1

Having links relative to root?

Is there a way to have all links on a page be relative to the root directory?
For example, on www.example.com/fruits/apples/apple.html I could have a link saying:
Back to Fruits List
Would this link be pointing to www.example.com/fruits/apples/fruits/index.html or www.example.com/fruits/index.html? If the first, is there a way to have it point to the 2nd instead?
A root-relative URL starts with a / character, to look something like link text.
The link you posted: Back to Fruits List is linking to an html file located in a directory named fruits, the directory being in the same directory as the html page in which this link appears.
To make it a root-relative URL, change it to:
Back to Fruits List
Edited in response to question, in comments, from OP:
So doing / will make it relative to www.example.com, is there a way to specify what the root is, e.g what if i want the root to be www.example.com/fruits in www.example.com/fruits/apples/apple.html?
Yes, prefacing the URL, in the href or src attributes, with a / will make the path relative to the root directory. For example, given the html page at www.example.com/fruits/apples.html, the a of href="/vegetables/carrots.html" will link to the page www.example.com/vegetables/carrots.html.
The base tag element allows you to specify the base-uri for that page (though the base tag would have to be added to every page in which it was necessary for to use a specific base, for this I'll simply cite the W3's example:
For example, given the following BASE declaration and A declaration:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Our Products</TITLE>
<BASE href="http://www.aviary.com/products/intro.html">
</HEAD>
<BODY>
<P>Have you seen our Bird Cages?
</BODY>
</HTML>
the relative URI "../cages/birds.gif" would resolve to:
http://www.aviary.com/cages/birds.gif
Example quoted from: http://www.w3.org/TR/html401/struct/links.html#h-12.4.
Suggested reading:
http://www.motive.co.nz/glossary/linking.php
http://www.communitymx.com/content/article.cfm?cid=AEDCC52C4AD230AD
Use
Back to Fruits List
or
Back to Fruits List
If you are creating the URL from the server side of an ASP.NET application, and deploying your website to a virtual directory (e.g. app2) in your website i.e.
http://www.yourwebsite.com/app2/
then just insert
<base href="~/" />
just after the title tag.
so whenever you use root relative e.g.
<a href="/Accounts/Login"/>
would resolve to "http://www.yourwebsite.com/app2/Accounts/Login"
This way you can always point to your files relatively-absolutely ;)
To me this is the most flexible solution.
Back to Fruits List
Relative Path Summary (applicable to href, src etc.,):
/file_Or_FolderName Root directory
./file_Or_FolderName Current directory
../file_Or_FolderName Previous directory (One level up)
../../file_Or_FolderName Previous of previous directory (Two levels up)
../../../file_Or_FolderName Just like above - Three levels up
Example:
www.example.com
├── apple.html
└── FolderA
├── fileA.html
└── FolderB
├── fileB.html
└── FolderC
├── fileC.html
└── FolderD <------ Suppose you're here (current directory)
├── fileD.html
└── FolderE
└── fileE.html
Following shows how to access the file at different levels using the relative path (applicable to href, src etc.,)
fileD.html - same level access(or)
./fileD.html - same level
./FolderE/fileE.html - 1 level Down
../fileC.html - 1 level Up
../../fileB.html - 2 levels Up
../../../fileA.html - 3 levels Up
../../../../apple.html - 4 levels Up (or)
/apple.html - 4 levels Up but direcly using root /
To give a URL to an image tag which locates images/ directory in the root like
`logo.png`
you should give src URL starting with / as follows:
<img src="/images/logo.png"/>
This code works in any directories without any troubles even if you are in branches/europe/about.php still the logo can be seen right there.
Use this code "./" as root on the server as it works for me
Back to Fruits List
but when you are on a local machine use the following code "../" as the root relative path
Back to Fruits List