How to set .htaccess so link in side html file will read from a base directory - html

Right now, I have put my html files under public/views folder,
libraries under public/libs, and customized files in public/src
I am using the DictoryIndex to set the default home page. Like DirectoryIndex public/views/index.html
but inside the index.html file, I have to use to open the file under the same directory as the index.html, otherwise it could not find the file. And then the URL will look ugly, like site.com/public/views/product.html.
Can I ask, how could I set the .htaccess file to get rid of the public/views in the URL address?
One more question, when i write the code to import the libraries in index.html, it seems like it will read from the base folder (like where I put the .htaccess file). But in the other html files, they will read from their own directory, like the html file in public/view will start to read from folder views. Is there anyway to solve this problem?
Thank you

Do you need to access your libs and custom files through HTTP too? If not, just set your document root to public/views
Otherwise, you could rewrite *.html to public/views using something like
Options +FollowSymLinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule (.+\.html)$ /public/views/$1 [L]
Anyway, using a fullpath instead of just a filename in the DirectoryIndex directive is not a good idea, for obvious reasons.
It also solves your second issue. For now, public/views/index.html is considered as / so anything relative to this file is relative to /, not the views folder. On the other hand, any other file is still considered as "itself", therefore includes are relative to the views folder.

Related

how to remove the url extension

I am developing a website for myself, pure and without server language.
The urls are as they are called in the a tag, for example: blog / index.html.
Is there anything I can use to change that? I don't want to have to use framework. I would like to know if there is any tool that does this.
What I want to change is the ending. I don't want the extension to appear, just the route
try create .htaccess file on your root folder then paste this
#remove html file extension-e.g. https://example.com/file.html will become https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
you can read more about this here https://www.plothost.com/kb/how-to-remove-php-html-extensions-with-htaccess/
EDIT
Since you are using Vercel.com, as per their documentation there's a file config named vercel.json you can add this
{
"cleanUrls": true
}
the docs said,
When set to true, all HTML files and Serverless Functions will have their extension removed. When visiting a path that ends with the extension, a 308 response will redirect the client to the extensionless path.
for more information pls read their docs here https://vercel.com/docs/configuration#project/clean-urls

Pointing webserver to load .html page and not subdirectory index

So I have been searching for a few hours now and have not found the answer to my problem.
So I have a website that I was wanting to take the page extension off e.g. .html / .php etc.. and I had found that I could include the below code in the .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I have now added that code and it works great for removing .html from my pages. However now I have come into a new problem. I have a page and directory both named 'guides' on the root of my public_html folder. Now when I enter the url 'www.mydomain.com/guides' the index of the guides directory is showing rather than the guides.html page that I am wanting to display.
I am wondering what would be the best way to get around this and make my server look for the webpage first rather than display the guides directory index.
I have been looking at RewriteRule's but it seems to be all wordpress content and does not work with my non wordpress website.
Any help would be appreciated, thanks.
The best solution is to change the name of the directory or the file to something different. RewriteRule can not rewrite a directory request to file as the directory request is first handled by mod-dir (directory module) .
If you do not want to change the name, then you can turn off DirectorySlash on your server using the following line in htaccess :
DirectorySlash off
https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash

How to change example.com/blog.html to example.com/blog

www.tinynerdgames.com/blog.html
I want this page's URL to not end with /blog.html rather with /blog. So the wished end result would be: www.tinynerdgames.com/blog
What do I have to do?
Any answer is highly appreciated, thank you.
You can use the .htaccess file to truncate your file URL's.
Navigate to your File Manager in cPanel and locate the .htaccess file. Note: you must enable hidden files to view this file.
Click on Settings at the top right corner
Select Show hidden files (dotfiles)
Save new settings
Inside the htaccess file, paste this code;
#remove html file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Now change your link from to https://tinynerdgames.com/blog.html to https://tinynerdgames.com/blog and this should work perfectly.
Using FTP, you would log in to your website, create a directory named blog, move the blog.html file into the new directory, and rename blog.html to index.html.

How can i remove .html from static html website pages

I have a static website with static files (pages), I would like the URLs of the pages not to contain .html at the end, for example:
"www.mywebsite.com/page.html" to become "www.mywebsite.com/page"
of course I can't just redirect the page, since the file "www.mywebsite.com/page" isn't known
Thanks in davance
I would recommend you use URL rewriting for your server's config file.
For example if you are using an apache server, you could use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html[NC,L]
in your .htaccess file (which is the config file for your apache server.

Clean/Short URLs using .htaccess is not working as it should

I've been following some YouTube videos to try and get this working but it doesn't seem to want to. This is how my .htaccess file is looking:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule RewriteRule ^(.*)$ index.php/$l
I am completely new to .htaccess, I'm not completely sure how it works but my common sense tells me to just create a file, call it .htaccess and place it in the same folder as my index.php, right?
So when I go to, say index.php/foo/bar or even just /foo/bar/ I get a 404 error.
What are the possible problems? It's a Windows 2008 Server with PHP 5.3.10 installed.
As beginning your last line should be
RewriteRule ^(.*)$ index.php/$l
And it should work as you expect it... pass all requests but ones for existing files/folders into index.php
The .htaccess can be in any folder which is hit by the request URL... the only difference is the base of the URL which is relative to the folder where is the .htaccess located
Example:
/folder1
.htaccess
index.php
/folder2
.htaccess
index.php
If you access /test.php none if your .htaccess files will be processed (it's not related to the request URI.
If you access /folder1/test.php the corresponding .htaccess in folder1 will be used and the $1 in your RewriteRule will show test.php, the /folder1/ will be stripped from the URI.
If you access /folder1/folder2/test.php it's similar... apache will use .htaccess from folder2... the one from folder1 will be ignored (a bit counderintuitive)