.htaccess removal of extensions - html

I'm having some troubles with my .htaccess file, I have code placed in which removes the extensions of files so it looks like this: www.foo.com/x and it works fine for all of my pages. However, when I use my navigation bar it doesn't work. I've already changed the URLs on each page to www.foo.com/x, so I'm confused as to what my problem could be. Here's the code in my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
And here's my nav-bar code:
<ul id="menu">
<li>Home</li>
<li>Projects</li>
<li>About</li>
<li>Contact</li>
</ul>
I might add that www.foo.com/projects, etc. works fine and goes to the correct page also my .htaccess file is in the root folder if that wasn't obvious.

Your original rules are going to cause a loop if you try to internally rewrite them back. For example, if you start off with the URI /some/file.html
Browser requests /some/file.html
RewriteRule ^(.*)\.html$ /$1 [L,R=301] matches, browser is redirected to /some/file
Browser requests /some/file
Internal rewrite to /some/file.html
Rewrite engine loops, first rule matches again, browser is redirected to /some/file
repeat from #3
So you need to match against the actual request for your first rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.html
RewriteRule ^ /%1 [L,R=301]
This does the redirect, in case you have links that still go to the .html files, now you need to internally rewrite them back to html files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ /$1.html [L]
If all of your links already have the .html part removed, then you won't need the first rule at all.

Related

Website links won't work with .htaccess code which removes .html extension

I have the following code in my .htacess file for my website to remove '.html' from my urls and to redirect any .html to the same page without it (example.com/home.html to example.com/home)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
However, when I try to click on links that are pointed to other pages on my website, I get a 404 error.
I've tried setting the href to http://www.example.com/page2, /page2 and more but I still get the same error. Any help?
Please save your code and put only the following code in your main directory .htaccess file :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(.*?)\.html[\s?/] [NC]
RewriteRule (.*)$ /%1%2 [R=302,L,NE]
Note: clear your browser cache because you did 301 redirect and test the code

Links (without /index in them) are not pointing to index of that subdirectory

I've looked for about two hours for a way of solving, this to no avail:
Just to make it easier I'm providing links: malumkose.com malumkose.com/hizmetler
In the main page (first link) there is a button link to the page "hizmetler."
When I was linking these pages to one another, I didn't add in the /index or /index.html extensions to the links, and thought it would work on the server as it did on localhost.
The thing is, my site is peppered with these links - they're all over the joint! I would go and manually change these to add the file extension, but that would only cause the link to direct me to "hizmetler/index" instead of just "hizmetler."
I have tried adding the first line here to my .htaccess file, but it still doesn't point links without index in them, to the indexes of these subdirectories.
`DirectoryIndex index.html
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]`
How could I possibly kill these two birds with one stone?
I don't completely understand the structure for the files of your pages, but this should help for your .htaccess file:
Options +FollowSymLinks
RewriteEngine On
DirectoryIndex index.html
RewriteRule ^hizmetler$ ./hizmetler/index.html
RewriteRule ^something$ ./something/index.html
RewriteRule ^something-else$ ./something-else/index.html
Then it will work as follow:
malumkose.com/ > /index.html
malumkose.com/hizmetler > /hizmetler/index.html
malumkose.com/something > /something/index.html
malumkose.com/something-esle > /something-else/index.html
If you want to force SSL/HTTPS, then add (between the enter of the above script):
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

How to make .html appear as / in .htaccess

My problem is this,
I want for example:
Make www.mywebsite.com/index.html
Look/appear (not redirect) like this
www.mywebsite.com/index/
so far I used below code and only obtained www.mywebsite.com/index look.
I will highlight that I don’t want to redirect, I want only to the html look like folder
I work in Adobe MUSE. I build websites mostly for fun and just want to focus on designing not coding
Here is my .htaccess script taken from here: .htaccess rewrite /foo/bar/ to /foo/bar.php
Please, can someone help me with it?
What line(s) should I change to have the folder appearance
Thanks
Options -MultiViews
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Za-z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

redirect subfolder to root with <subfolder-name>.html using htaccess

I have website that has links like
domain-name.com/pages/contact-us/
www.domain-name.com/pages/contact-us/
domain-name.com/pages/about-us/
www.domain-name.com/pages/about-us/
and other pages ...
What I'm looking for how i can redirect them permanently to same subfoldername.html using htaccess . for example for all the links above should redirected to
domain-name.com/contact-us.html
www.domain-name.com/contact-us.html
domain-name.com/about-us.html
www.domain-name.com/about-us.html
also for other pages, otherwise if page not found to root index.html .
Thanks in advance.
Try the following
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/pages/(.*[^/])/?)$
RewriteRule ^(.*)$ /%2.html [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.html [L,R=302]
The first three lines will remove the folder pages. If there is a trailing slash, it will be removed. The last three lines will redirect to /index.html, if the request file does not exist.

Access CSS file after htaccess modified url

I am using this htaccess file to re-write my urls:
RewriteEngine On
# Friend SEO url
#RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.php
#RewriteRule ^ /%2/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
The url rewriting works perfectly fine (for example, mypage/web-store/ can be used instead of mypage/web-store.html).
The problem however, is that my rewritten url can't retrieve the CSS file correctly. If I use the normal url however, I have no problem at all because the CSS file is in a subfolder of mypage. But with the url rewrite, it looks for the css file inside the folder /web-store/, which doesn't exist...
Anyone knows a way to counter this ?
You need to either change your URLs to absolute URLs (add a leading /) or you can set a relative URL base in the header of your pages:
<base href="http://example.com/mypage/" />
Where "example.com" is the domain name of your site and the /mypage/ is where the css is at.
If all else still fails, you can try to rewrite the proper page:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+\.css)$ /path/to/styles/$1 [L,R=301]