How do I redirect "website/page" to "website/page.html" when people type it in browser (pretty-url) - html

I recently started using a static site with godaddy.com without a cms. It's a basic website I uploaded and is only html/js/css.
When people type "website.com/page" they get 404 not found.
When they type "website.com/page.html" they get the correct page.
How can I fix my site so users can simply type "website/page" and not be forced to type "website/page.html" without using php?
SOLUTION
After researching the information about .htaccess files provided by #Gijsberts (thank you), I did the following:
Created a .htaccess file on server.
Added the following code:
# Remove the .html extension from html files
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]

Most CMS systems have a .htaccess file what handles the redirect and the 404 errors. In CMS system they are most called "Pretty-urls". Read the links below to set up those pretty urls yourself with a static web page without a cms.
https://mediatemple.net/community/products/grid/204643080/how-do-i-redirect-my-site-using-a-htaccess-file
http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049

You normally can do this with your Webserver config. In Apache you have this enabled automatically. In nginx you should have a look at this tutorial.
If you are using caddy you have to config your caddyfile like
ext .html .htm .php
if you are using microsoft iis have a look at microsofts tutorial.
Let me know if you are using something else or need more help.
EDIT
If you are using a webspace you may are not able to change the config of the webserver. you now can use .htaccess. create a .htaccess file and write down something like
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Related

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.

Errors with htaccess

Today I tried to write a .htaccess file for first time in order to remove .html extension from url bar along with some other things that I wanted to do. Since I was unfamiliar with all this I read several articles before coding.
I ended up with the following code. I also removed .html from all links. The problem is that when visiting my domain I get the following error.
//The resource you are looking for has been removed,
//had its name changed, or is temporarily unavailable.
Is my code correct?
UPDATED .htaccess
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
//Remove .html UNTIL NOW THIS IS THE ONLY PART
//OF THE CODE THAT ACTUALLY WORKS.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
//Caching schema
<FilesMatch "\.(jpg|png)$">
Header set Cache-Control "private, max-age=160704000"
</FilesMatch>
//Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
In a separate .htaccess
//Prevent directory listings
Options All -Indexes
MAIN ERRORS
If I click on link like:
Home
Then I get the same error.
Thank you all in advance.
This is native in Apache without using mod_rewrite by using MultiViews option.
Documentation states:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for 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.
I finally managed to resolve a part of the problem. For starters I changed my server from windows to linux.
Then to remove html extension I used this code in .htaccess (the other codes didn't work well, giving me an error message)
//Remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I also removed any .html instances from all links.
Now it works like a charm.

(HTTP) Hide page extension in the URL

I'm making a web page using my Raspberry PI and Apache. I made the page chat.html.
To connect to it I have to write in Chrome "http://192.168.1.39/chat.php". How can I hide the '.php'(or anything else) from page URL, so when I connect to "http://192.168.1.39/chat" directly it doesn't give Error 404?
Thank you in advance.
Use URL rewriting by utilizing .htaccess. Basically .htaccess is a file that let's you alter the config of a webserver like Apache.
In your web site root, create a filed called .htaccess.
Paste this in it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
*.php files can now be accessed the way you describe. For more info on htaccess and url rewriting, please reference http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

Redirect non-existant site pages via .htaccess

I have a problem right now with visitors browsing to pages that used to exist on my server but no longer. This is due to my initially poor decision to use a subdirectory instead of a subdomain. Because of this, people posted direct links to the pages themselve instead of the directory to various message boards. So when people Google and find links to my site, they click what the people have linked and it directs them to a "Not Found" page.
To better explain, here is my best attempt at giving an example.
The main URL people were using to visit my site was hxxp://example.com/subsite/. Within that /subsite/ directory there were files such as welcome.html, about.html, contact.html. So what happened is that when people started spreading my website links around, they would spread a link such as hxxp://example.com/subsite/welcome.html instead of just hxxp://example.com/subsite/. As of right now, the subsite directory only has (mainly) an index.html file.
What I am trying to accomplish is that whenever someone tries to navigate to any webpage within that subdirectory that doesn't exist, it redirects them to the index.html file. I'd like to accomplish this via the .htaccess file.
I attempted to do this myself by using the below code, but apparently I'm too new to how the .htaccess and apache modules work and the below code causes a constant redirect loop.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hxxp://example.com/subsite/ [L,QSA]
</IfModule>
If anyone has any input, or needs me to clarify any information just let me know. Any help would be greatly appreciated. Thank you!
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(subsite)/.+$ /$1/ [L,NC,R=301]

How to make URLs in a website appear solely as folders

I am trying to design a folder structure for a website project I am working on. A lot of sites these days seem to have the following link structure:
www.example.com/news/news-item-one/
www.example.com/about-us/
Can I make my site work like this without making a new folder for each page I have and putting an index.php file in it?
i.e www.example.com/news/new-item-one.php reads www.example.com/news/news-item-one/
You can use a web application framework like CodeIgniter or CakePHP to do URI routing for you:
http://codeigniter.com/
http://cakephp.org/
This is done with an .htaccess file which either of those frameworks can provide in example documents and they have extensive documentation about URI routing. For example on CI:
http://codeigniter.com/user_guide/general/urls.html
Yes, but it depends what server you are running.
If Apache, one of the most common ways is to create an .htaccess file and use rewrite rules to declare the different routes your website uses.
Below is a very simple example, although not necessarily the best way. There are things you can do to make it more flexible, but I believe it's out of scope of this question. For what it's worth, I prefer a catch-all route that passes route handling to my framework.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^news/(.*)$ news.php?item=$1 [NC,L,QSA]
RewriteRule ^about-us/$ about-us.php [NC,L,QSA]
</IfModule>
You want to use htaccess.
Create a file in your root directory called .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1.php [NC]
You don't need to use a framework (although it may be a good idea). You can simply setup your .htacess correctly:
http://www.evolt.org/Making_clean_URLs_with_Apache_and_PHP
The following mod_rewrite code (to put in your Apache configuration) will allow you to hide the .php extension of any page you have on your site.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [L]
It checks to make sure something isn't a file or directory itself, then that adding .php after it's name actually is a file, and it serves that instead.
So if you have a /page.php on your site, going to /page will be the same as going to /page.php.