How to get back to index.html in .htaccess? - html

I am completely new writing in .htaccess format. First what I did was add this in for the removal of my .html extensions. I am doing all this in cPanel under the /public_html/ folder. I also moved my .htaccess file inside that folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
All my pages that I have up right now are working without the extensions, expect when I go back to my homepage. Originally index.html but now when I click the home button my url looks like http://allisonshtml.com/public_html
Really to me when I think about this, its kind of backwards because my website is inside the public_html folder.
I tried everything in my power to fix this. Please help or any feedback would be lovely!

Figured it out, my Google chrome wasn't rendering. Cleared the background data and waaalaa it worked.

Related

Firefox uses wrong urls

I coded a webpage. At all sites I have to include several files via HTML. These files are stylesheets, images, javascripts and so on.
My big problem is Firefox. If I open the main page (https://www.example.de/) in any browser everything works fine. If I open a subpage (https://www.example.de/sub_page) of the webpage in Microsoft Edge, Chrome, Safari and a few other browsers everything displays as it should. Only in Firefox the subpage completly displays incorrect. Over the network tab I found out that these files are not loaded, because Firefox uses wrong urls for the files I have to include.
For example:
Included JS:
<script src="./scripts/main.js"></script>
Expected example-URL (Used URL in all other browsers):
https://www.example.de/scripts/main.js
Used example-URL only by Firefox:
https://www.example.de/subpage/scripts/main.js
And that last one is a wrong url. The first one is correct.
The subpage file is in the same directory as the main site file and the main site file loads perfectly in Firefox with the same include-urls. So why only in Firefox the urls get like this at my subpage? Any help is highly appreciated. Please tell me if you need more examples or anything else.
EDIT:
The subpage is a single php file and not in a extra folder. .htaccess edits the urls on the whole site. It is looking like this:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
ErrorDocument 400 ./404.php
Best regards,
Filip.

Removing .html for index file in subfolder

I have just launched an English version of my website, adding an index.html file to an /en subfolder.
Now, I cannot access produktionskollektivet.com/en/ (it returns a 403 Forbidden), but have to link to produktionskollektivet.com/en/index.html.
I have a Rewrite rule in .htaccess that should remove the .html extension, but that doesn't seem to work in this instance.
Here is what I have in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Redirect 301 /hej-hbg.html /index.html
Any help would be appreciated!
Removing .html extension using .htaccess file:
DirectoryIndex somepage.php home.php welcome.php index.html index.php landingpage.html
~Put there where you want to remove the extension.
Note: save the file with the .htaccess extension.
This is easy & works for me, Hope it helps.

Rerouting file to folder/directory name, but preserving directory children in Apache

I have a folder and a file with the same name, for example main.html and main/. I have the following rules in my .htaccess file:
RewriteEngine On
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
These rules are made to remove all the .html extensions from the .html files when they are served to the browser.
How can I tell the browser to recognise that I want to view main.html without .html extension, not the main folder?
If this is not possible to do I am fine with having this main.html file in main folder, but when calling the main.html it should display the ../main/ instead of ../main/main.html. How would I do that?
If I specify the full path in the code like so: , browser will display the .html ending. But if I change it to browser says that there is nothing to load in the main/ and returns 403 Forbidden error.
Update:
RewriteCond %{REQUEST_FILENAME} main/$
RewriteRule ^(.*)/$ $1
Adding the rules above sort of works, but now I cannot access sub-directories in main directory because it gets redirected to main file.
Tried the following solutions without success:
https://stackoverflow.com/a/2017766/5153030
https://stackoverflow.com/a/1237547/5153030

Problems with CSS while using RewriteRule on .htaccess

I'm having some trouble while using RewriteRule on my local and remote servers.
Here's the situation I'm in right now:
localhost/puntvallesgit/index.php?seccion=nosaltres
this is the actual URL
I want it to be renamed to:
localhost/puntvallesgit/nosaltres
and
localhost/puntvallesgit/nosaltres/
Both urls work, meaning, it does not give me a 404 or something like that, but, with the second one (localhost/puntvallesgit/nosaltres/) there's no CSS nor JS loading correctly.
Looking the html code, I can see that while using the rule, the url path for JS and CSS gets changed to:
localhost/puntvallesgit/nosaltres/css/styles.css
yet the code itself has this:
<link href="./css/styles.css" rel="stylesheet">
I understand that sine I'm rewritting the url, to /nosaltres ... the html code adapts to that new folder structure, but that is not the expected behaviour.
Also, while using the url without the final slash (localhost/puntvallesgit/nosaltres) it works great (under localhost).
I could use absolute paths, yet, that is not the most ideal thing to do, since then paths would change from my localhost to my remote server
Here's the htaccess rewrite i'm using:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?seccion=$1 [NC]
RewriteRule ^([^/]*)/$ index.php?seccion=$1 [NC]
</IfModule>
Some advice please?
Thanks in advance
You can add the base tag between the <head> </head> section of your site. Then your asset folders will read from the root.
<base href="/">
Also rewritecond only works for the first rewrite following it. You can condense your rule this way.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]*)/?$ index.php?seccion=$1 [NC,L]
</IfModule>
This is not htaccess related (although your rewrite rules are not optimal), but rather a problem in the way you load your stylesheet.
When browsing to http://localhost/puntvallesgit/nosaltres/, the browser will try to load your stylesheet from ./css/styles.css which translates to http://localhost/puntvallesgit/nosaltres/css/styles.css, while your stylesheet is probably at http://localhost/puntvallesgit/css/styles.css.
You should use /puntvallesgit/css/styles.css as the source of your stylesheet instead.

remove .html with htaccess

I'm using my htaccess file to try to remove .html (and other if I need to) file extensions from my web URL's.
The problem is it doesn't seem to work unless I go through my links in the site and manually remove .html from all of them (which makes testing the site locally quite annoying and I'm not sure if I'm supposed to remove the .html file extension).
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
This works if a user types in www.mysite.com/about instead of www.mysite.com/about.html, except when clicking through the links of my site it still adds the extension?
What's the best way to go about removing the .html, absolute links on my site with http://www.mysite.com/about etc or should I remove the extension and just have paths like ../about instead of ../about.html?
.htaccess file does not change your code so if you put the extension in your page (for links), you must remove it.