How to hide file extensions in website
As written, this code does two things:. So both sides of the URL coin are covered. No edits are required for this technique to work. There are some conditions that need met, as explained in the next section. In general, to remove file extensions and replace them with clean permalink structure, three conditions must be met:. The first condition is important. To understand, consider a simple site that contains ONLY the following files:. But you, being savvy with SEO and usability, want to remove the file extension from each of those requests, so that they look more user-friendly:.
Because each of these permalink URLs corresponds to an existing. Notice the file name is used as the directory name in each of the permalink URLs. This is key to understanding how it works. So for example, if the following request is made:. Because there is not an existing file named page4. Hence the first condition: A file must exist for each URL.
But that is not to say that a unique file must exist for each URL. If you are using a dynamic language such as PHP , you can have one file that handles all requests. For example if you have an index. Then the first condition is met, because there is a file index. It works because there is a file to process each request. Quite simply, these directives check if the request is for a. Basically this rule checks if the request is for the permalink directory name; if yes, then the request is rewritten as the file-name URL.
When this rule is combined with those from the 2nd condition, all requests are accounted for: requests for the file will redirect to the permalink URL, and requests for the permalink URL will rewrite as the file-name URL.
The code basically is the same, with just a few subtle modifications. Or It has to be pasted into this. Let me tell you that this code can be used only for the website created by HTML. And if you have created a website with PHP, then you can use this code.
After using Save in this code in the. HTML and. PHP extension. You can also see the image given above. For more information, please watch this video. HTML extension using these codes. Even though the directory asdf doesn't exist. You want to do this so that the OLD ones work too - what you do is maintain a system by which the variable is always exactly the same OR create a database table that correlates the "SEO friendly URL" with the product id.
An example might be. The advantage to this? Search engines will index the keywords "Some Cool Video. Who's going to search for that? You have different choices. One on them is creating a folder named "profile" and rename your "profile.
Here is a simple PHP way that I use. If a page is requested with the. I came up with this solution because none of the many. Obviously you still need to have setup Apache to redirect any request without extension to the file with the extension. The above solution simply checks if the requested URI has an extension, if it does it requests the URI without the extension. Then Apache does the redirect to the file with the extension, but only the requested URI without the extension is shown in the browser's address field.
The advantage is that all your "href" links in your code can still have the full filename, i. And then the URL www.
If you want to take it further, open a subfolder e. The solutions are 1. I think you include a menu in each php file probably your menu is showed in all your web pages , so you can add these 2 lines of code only in your menu file. This work for me :D. Remove a file extension through.
For those who are still looking for a simple answer to this; You can remove your file extension by using. Because when user copies the URL from address bar or tries to reload or even coming back from history, your standart Apache Router will not be able to realize what are you looking for and throw you a Error.
You need a dedicated Router for this purpose to make your app understand what does the URL actually means by saying something Server and File System has no idea about. I leave here my solution for this.
This is tested and used many times for my clients and for my projects too. It supports multi language and language detection too. Read Readme file is recommended. It also provides you a good structure to have a tidy project with differenciated language files you can even have different designs for each language and separated css , js and php files even more like images or whatever you have.
0コメント