What is the simplest way to remove .html with an htaccess file? - .htaccess

So I have a been trying a lot of different htaccess codings to clean up my url
but am not even able to simply remove .html without something going wrong
Code Im using:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
CODE Im hoping to clean up:
<area shape="poly" coords="3,612,78,557,118,627,5,708" href="collabwitus.html" />
Little new to htaccess and would love some help!
I am doing everything from file manager via hostgator so framing an answer in a more step by step version using file manager would be great!

Put the following code :
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

This code worked for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Related

Why does echoing htaccess-value end with file-ending?

I have created an htaccess-file to hide file-endings and prettify URLs. Now trying out everything I witness echoing a value from a prettified URL shows the file-ending in the result.
This varied a lot as I wasn't sure where the problem is. So there was ([0-9a-zA-Z]+) for example. I have also tried solutions from StackOverflow.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^events/([^/]+)$ viewevent.php?c=$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And my viewevent.php-file
<?
echo $_GET["c"];
?>
I used to try different variations, but it's either error or with the file-ending. And the URL I enter is: url/events/bino.
I expect bino, but I get bino.php.
Where is the problem?
Your rules need more reordering otherwise in present state it will redirect http://example.com/events/foo to https://example.com/viewevent.php?c=foo thus exposing your internal handler.
Moreover you need to add .php for files that actually exist in your filesystem as php file.
You may use this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/([^/]+)/?$ viewevent.php?c=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The answer was simple. Put RewriteRule ^([^\.]+)$ $1.php [NC,L] at the end of the file, else it's kind of mixing with the other rules.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/([^/]+)$ viewevent.php?c=$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

.htaccess multiple rewriterules conflicts

I have some problems with .htaccess here is the code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
the problem is that removing .php extension part does not work. If I move it to the top, then "SEO" links does not work. Please, can someone help me to fix this?

Multiple rewrite rules and conditions in htaccess file

In my .htaccess file i have already some rewrite conditions and rules, and its working normal.
Now i need to add "http to https redirect" to my htaccess file.
Here is my old .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([a-z0-9_-]+)$ pages/$1.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2/$3.php [L]
I attempted to add below code to file but it doesnt work properly.
For example if i write the url direct to browser its work (only with www). But if click my any backlinks or google search result links it doesnt work.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
Where do i wrong? Where should i put the code? Any help would be great. Thanks.
your htaccess could look like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([a-z0-9_-]+)$ pages/$1.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2.php [L]
RewriteRule ^([a-z0-9_-]+)/([-a-zA-Z0-9_]+)/([-a-zA-Z0-9_]+)$ pages/$1/$2/$3.php [L]
At least, it works for me, in my own implementation
You can use the following htaccess :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)/?$ $1.php [L]
Clear your browser's cache before testing this

Remove php file extension

I am trying to implement url this way localhost/mysite/contact/ without php extension and adding a trailing slash at the end but it is not working.
This is the code sample:
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
How can I implement the following:
localhost/mysite/page/
Any help please?
Thank you.
Ensure you have mod rewrite enabled and loaded
Eg
RewriteEngine on
this will work without the file v system checking overhead
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^foo/?$ bar [L,NC,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/mysite/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

htaccess works but content doesn't load

When I use index.php?id=this-is-an-article the page loads with the content, when I use /articles/this-is-an-article the page loads without any content, any idea as to how I can resolve this?
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# force www. in all requests
RewriteCond %{HTTP_HOST} ^mysite\.net [NC]
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
# enable hiding php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)\$ $1.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^page/(\d+)*$ ./index.php?p=$1
# index.php?id=this-is-an-article => /articles/this-is-an-article
RewriteRule /articles/(.*) index.php?id=$1 [NC,L]
Thanks in advance.
Get rid of the leading slash. It's also best practice to begin a regex like that with ^ and end it with $.
RewriteRule ^articles/(.*)$ index.php?id=$1 [NC,L]

Resources