How to hide .html extension from the website url - php

I know this question has been asked before but does anyone know of a good way to hide .html extensions. I've tried many of codes & many of the answers from the https://stackoverflow.com/ but am not seeing the result. Thats y I ask u again
I've a static website and I wanted to remove the extension to clean my urls. I was working with static html files.
Prior to removing the extension, the url would read website.com/about.html.
With the extension removed, it would look like website.com/about. Hope that was clear.
I've a .htaccess file and I tried many of the codes but it dosen't work. Here are some codes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\ [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L]
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteBase /
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.html [nc]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
however I am not seeing any results...:(

You have your rule reversed (the first one would work otherwise):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
Your original rule was RewriteRule ^(.*)\.html$ $1 [L] which translates to "If the requested resource name ends with .html then issue a redirect to the browser telling it to ask us if we have anything at the same name without the HTML". This results in a 404 (since Apache doesn't have anything to serve for the .html'less resource).
By switching the .html to the destination RewriteRule ^(.*)$ $1.html [L] we change the rule to say "If the requested resource name is not a file or directory on disk then try to re-route the request (internally, don't tell the browser) as if it ended in .html".

Note that you do not need complex (by nature) RewriteRules for such task.
The mod_negotiation apache module, often enabled by default, provides such behavior with Multivews option.
If the server receives a request for /some/dir/foo and /some/dir/foo does not exist, then the server reads the directory looking for all files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements, and returns that document.
.i.e requesting for foo/bar will serve foo/bar if it us a directory and will seak for foo/bar.html, foo/bar.txt and such etc if not.
You just need to ensure this option is activated in your current context (a Directory for example)
Options +Multiviews

use this
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L,QSA]

Look at this post http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ I haven't tried it yet but the demonstration seems pretty convincing

Related

HTAccess Rewrite Issues

I am writing a website which is dynamically populated through an Oracle database.
I have completed the desktop site and am now required to create a mobile site. Due to how different the sites are planned to look, I have opted to create 2 different "template" like websites for the mobile and desktop sites.
However, for my desktop site, everything is built off the index.php file in order to allow it to be completely dynamic. Pages are therefore look like www.domain.com/index.php/page in the url.
For the desktop site, this works. I am using a generic index.php removal rewrite rule in order to then make the url www.domain.com/page however still display the same page as the previous URL.
My issue, is that now I have a www.domain.com/mobile/index.php. Which has been created and for the most part has been working, however when trying to add addition dynamic pages to the mobile site. www.domain.com/mobile/index.php/about for example just redirects to www.domain.com/mobile/ and it doesn't even include the about part of the URL.
After much debugging, I have discovered it is definitely the .htaccess that is causing the issue.
If you have any insight into my issue, please help me out.
Thanks in advance
EDIT 1
Rewrite Rules are as follows
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
You can use this code in your /mobile/.htaccess file:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /mobile/
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ $1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
This will override all the rules present in parent .htaccess for /mobile/ URI path.
Simplified version to make it work in root .htaccess:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(mobile)/(.*)$ $1/index.php/$2 [L,NC]
# Directs all EE web requests through the site index file
RewriteRule ^(.*)$ /index.php/$1 [L]
Based on the debugging you mentioned, there is a rule in your .htaccess which is rewriting www.domain.com/mobile/index.php/about to www.domain.com/mobile/. So, if you find which rule this is, you can add one above it that will catch requested URLs for your mobile pages and then not allow the problematic following rule to run. Something like this:
RewriteRule ^mobile/index.php/([a-zA-Z0-9-_]+)$ ^mobile/index.php/$1 [L,QSA]
The L ensures that if the user's request matches this rule, no further rules (including the one causing the issue) will be executed.
Thank you for the answers you've both given, however neither of them worked, I've now solved the issue, it was to do with the final RewriteRule at the end
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I needed to change it to
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/mobile/.* [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /mobile/.* [NC]
RewriteRule ^(.*)$ mobile/index.php/$1 [L]
So that it would work with both mobile and desktop sites.

.htaccess not rewriting the URL correctly for .php files, but OK for .html files

I'm trying to accomplish 2 things in my .htaccess:
Redirect all requests for (in example) www.blanklabs.com/boarddrive/faq, www.blanklabs.com/boarddrive/faq.htm, www.blanklabs.com/boarddrive/faq.html, or www.blanklabs.com/boarddrive/faq.php to www.blanklabs.com/boarddrive/faq.php
The browser's address bar should show just www.blanklabs.com/boarddrive/faq, without the extension.
Here is my current .htaccess:
RewriteEngine On
# -- new
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php [L,QSA]
On the server I have faq.html (for now), but I also tried having both faq.html and faq.php. Eventually it'll just be faq.php.
The .htaccess is clearly incorrect, since if I go to www.blanklabs.com/boarddrive/faq.html I get the correct content (from faq.html), but if I go to www.blanklabs.com/boarddrive/faq.php I get a 500 error. This happens even if I have faq.php on the server.
Any ideas what I'm doing wrong? The no-extensions is secondary, the primary goal is to redirect all requests from html to php files.
Place this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# skip POST requests
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(php|html?)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)(\.html?)?$ /$1.php [L,QSA]
You need to redirect
/faq.htm
/faq.php
[using redirect directive]
to /faq
now just applying rewrite rule to this later condition [/faq] to
/faq.php
It should work.

htaccess rewrite to a script when the requested file does not exist

I hope you can help. I'm trying to write a .htaccess file to do the following.
redirect to www. address
remove .php from URL
If the file doesn't exist then use filechecker.php?page=filename
#1 I can do with
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#2 I can do with
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule [^/]$ %{REQUEST_URI}.php [QSA,L]
#3 I thought
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^([^/]*)$ filechecker.php?page=$1 [QSA,L]
would work, but for some reason it is ignoring the fact that the page does actually exist.
Your solution for 2 will loop, but simple enough to fix, stick something along the following lines in your file for part 2 and 3:
#If the Browser request contains a .php, instruct the browser to remove it.
RewriteCond %{THE_REQUEST} \.php [NC]
RewriteRule ^/?(.*)\.php$ http://%{HTTP_HOST}/$1 [R=301,NC,L]
#If a request is received for a non file-system object, that doesn't have a .php suffix, then store the full path, filename, and URI in a variables with that extention.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-s
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !^\.php$ [NC]
RewriteRule ([^/]+)$ - [E=testScript:%{SCRIPT_FILENAME}.php,E=testFile:$1.php,E=testURI:%{REQUEST_URI}.php]
#See if the file exists with a .php extention, if it does internally rewrite
RewriteCond %{ENV:testScript} -s [OR]
RewriteCond %{ENV:testScript} -f
RewriteRule .* %{ENV:testURI} [L]
#Else if a ENV:testDile is set, then pass the name to the php script
RewriteCond %{ENV:testFile} !^$
RewriteRule .* /filechecker.php?page=%{ENV:testFile} [L]
FYI: The Apache mod_rewrite documentation is worth a read, if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.

Removing file extension apache server

today i wanted to achieve this thing where .htaccess automatically removes any sort of file extension from the url on the browser.
Lets say for example user clicks a link to take the user to
mylink.php
It should take him there but remove the .php
i tried using a few .htaccess code but they did not do much.
Also i can access the pages like so
localhost/register << this one works fine but i want the extension to be removed auto
localhost/register/ << this one looses all styling because it behaves like a page in new folder
The .htaccess code is
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Any ideas people?
RewriteEngine On
# Change this to the appropriate base (probably just / in your case)
RewriteBase /stackoverflow/9762238/
# Any url that requests a .php file on the base will be redirected to dir with same base name
# The condition is important to prevent redirect loops!
RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteRule ^(.*)\.php$ $1/ [R=301,L]
# Requests for directories that do not actually exist will be be translated into php file requests
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/$ $1 [L]

how to hide .php from address bar

I use php to build web applications, but i want my web pages without .php extension in the browser's address bar. For eample http://www.example.com/index.php shows like http://www.example.com/index in the browser's address bar.
How can i do this?
Put this in a file named .htaccess in your WWW-root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
This works if you're running Apache and have mod_rewrite activated.
Just create a .htaccess file in wamp/www/ and copy-paste this code..
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
Hope! this would be useful for someone!!
You'll want to find the appropriate method of url-rewriting for your web server. It lets you map
www.domain.com/page
to
www.domain.com/page.php
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally redirect /dir/foo/ to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Just to point out that on older versions of IIS for example IIS6 and assuming you are in a 32 bit process then IONICS ISAPI Rewrite is a fantastic free url rewriting module. Inside of 64 bit in IIS 6 I have found the commercial product Helicon ISAPI Rewrite 3 to be a great tool. But if you are in 32 bit, IONICS is free and does everything you will require.
http://iirf.codeplex.com/
See Change URL Address make short in PHP
In apache2.conf I have
<Files data>
ForceType application/x-httpd-php
</Files>
Which means data is treated as a PHP file without the extension
There are several ways of doing it.
You can use mod-rewrite to rewire foo to foo.php so that requests for /bar gets handled by /bar.php.
You can use directories, and default-files, so that you link to the direcory /foo/ which gets handled by /foo/index.php
You can set a php-script as the handler for 404-errors, then you just link to nonexistant files, and the handler-file deals with it however it likes. (typically by using some sort of map from url to php-file)
You can tell your webserver that all request for a certain webserver, is to be handled by php.
The first or second solution is the simplest, but the 2 last ones gives the best flexibility, and variants thereof is what most of the bigger frameworks do.
On systems using the Apache webserver, you would use mod_rewrite.
On newer versions of IIS, you can use their version of mod_rewrite. On older versions you need to buy a plugin.
Stack Overflow article
Search Stack Overflow and you should find answers to questions already asked.
Here is a beginners tutorial for URL rewriting.
little modification - better to use REQUEST_URI and check for the directory
# Turn mod_rewrite on
RewriteEngine on
# To externally redirect /dir/foo.html to /dir/foo/
RewriteCond %{REQUEST_URI} ^(.+)\.html?$ [NC]
RewriteRule ^ %1 [R,L]
# To internally redirect /dir/foo/ to /dir/foo.html
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
# To externally redirect /dir/foo.html to /dir/foo/
RewriteCond %{REQUEST_URI} ^(.+)\.html?$ [NC]
RewriteRule ^ %1 [R,L]
# To internally redirect /dir/foo/ to /dir/foo.html
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
you can use this code for hide .php and if isset .php show error
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
# Return 404 if original request is .php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
Insert this code into your .htaccess file on the remote server:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This will rewrite the URLs in the way you intended.

Categories