I recently wanted to remove the extensions from my website, in order to make the URLs more user and search engine friendly. I stumbled across tutorials on how to remove the .php
extension from a PHP page. What about the .html
? I wanted to remove those as well! In this tutorial I’ll show you how to do that easily, by editing the .htaccess
file.
What is an .htaccess file
An .htaccess
file is a simple ASCII file that you
create with a text editor like Notepad or TextEdit. The file lets the server know what configuration changes to make on a per-directory basis.
Please note that
.htaccess
is the full name of the file. It isn’t2, it’s simplyRewriteRule ^[[^\.]+]$ $1.html [NC,L]
.htaccess
.
.htaccess
files affect the directory in which they are placed in and all children [sub-directories]. For example if there is one .htaccess
file located in your root directory of
RewriteRule ^[[^\.]+]$ $1.html [NC,L]
6, it would affect
RewriteRule ^[[^\.]+]$ $1.html [NC,L]
7, RewriteRule ^[[^\.]+]$ $1.html [NC,L]
8, and so on…It is important to remember that this can be bypassed. If you don’t want certain .htaccess
commands to affect a specific directory, place a new .htaccess
file within the directory you don’t want to be affected with the changes, and remove the specific command[s] from the new file.
Features
With an .htaccess
file you can:
- Redirect the user to different page
- Password protect a specific directory
- Block users by IP
- Preventing hot-linking of your images
- Rewrite URLs
- Specify your own Error Documents
In this tutorial we’ll be focusing only on rewriting URLs.
Removing Extensions
To remove the .php
extension from a PHP file for example
wallpaper
3 to wallpaper
4 you have to add the following code inside the .htaccess
file:RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[[^\.]+]$ $1.php [NC,L]
If you want to remove the .html
extension from a html file for example
wallpaper
7 to wallpaper
4 you simply have to change the last line from the code above, to match the filename:RewriteRule ^[[^\.]+]$ $1.html [NC,L]
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page. For example:
wallpaper
Adding a trailing slash at the end
I received many requests asking how to add a trailing slash at the end, for example:
wallpaper
9Ignore the first snippet and insert the code below. The first four lines deal with the removal of the extension and the following, with the addition of the trailing slash and redirecting.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
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]
Link to the HTML or PHP file the same way as shown above. Don’t forget to change the code if you want it applied to an HTML file instead of PHP.
Some people asked how you can remove the extension from both HTML and PHP files. I don’t have a solution for that. But, you could just change
the extension of your HTML file from .html
or
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
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]
1 to .php
and add the code for removing the .php
extension.Conclusion
For those who are not so experienced with .htaccess
files there is an online tool for creating them. It’s useful for novice users to get started, and easy to use.
Updates
Attention GoDaddy users: In order to remove the extensions you need to enable
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
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]
5 before. The code should look like this:Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[[^\.]+]$ $1.php [NC,L]
If you’re worried that search engines might index these pages as duplicate content, add a
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
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]
6 meta tag in the RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
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]
7 of your HTML file:
Câu hỏi hay, nhưng dường như nó đã khiến mọi người bối rối. Các câu trả lời gần như được phân chia giữa những người nghĩ rằng Dave [OP] đang lưu các trang HTML của anh ta mà không có phần mở rộng .html
và những người nghĩ rằng anh ta đang cứu họ như bình thường [với .html
], nhưng muốn URL xuất hiện mà không cần. Mặc dù câu hỏi có thể được nói tốt hơn một chút, tôi nghĩ rằng nó rõ ràng ý anh ta. Nếu anh ta đang lưu các trang mà không có .html
, hai câu hỏi của anh ta [‘Cách xóa .html '] và [làm thế nào để chuyển hướng bất kỳ URL nào với. Vì vậy, việc giải thích không có ý nghĩa gì. Ngoài ra, nhận xét đầu tiên của anh ấy [về việc tránh một vòng lặp vô hạn] và câu trả lời của chính anh ấy dường như xác nhận điều này.
Vì vậy, hãy để bắt đầu bằng cách viết lại câu hỏi và phá vỡ nhiệm vụ. Chúng tôi muốn hoàn thành hai điều:
- Xóa rõ ràng
.html
nếu nó là một phần của URL được yêu cầu [ví dụ:
2]Options +MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^[[^\.]+]$ $1.php [NC,L]
- Chỉ ra URL bị cắt [ví dụ:
3] trở lại tệp thực tế [Options +MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^[[^\.]+]$ $1.php [NC,L]
2].Options +MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^[[^\.]+]$ $1.php [NC,L]
Không có gì khó khăn khi làm một trong những điều này. [Chúng tôi có thể đạt được thứ hai chỉ bằng cách kích hoạt nhiều lượt xem.] Thử thách ở đây là thực hiện cả hai mà không tạo ra một vòng lặp vô hạn.
Câu trả lời riêng của Dave đã hoàn thành công việc, nhưng nó khá phức tạp và không phải là di động. . Tuy nhiên, không ai trong số họ giải thích làm thế nào các giải pháp của họ đã giải quyết vấn đề cơ bản, cách thức tránh một vòng lặp vô hạn. Theo tôi hiểu, chúng hoạt động vì biến
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[[^\.]+]$ $1.php [NC,L]
5 giữ yêu cầu ban đầu từ trình duyệt. Như vậy, điều kiện [Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[[^\.]+]$ $1.php [NC,L]
6] chỉ được kích hoạt một lần. Vì nó không được kích hoạt khi viết lại, bạn tránh được kịch bản vòng lặp vô hạn. Nhưng sau đó, bạn đang đối phó với yêu cầu HTTP đầy đủ, ____________ 47, Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[[^\.]+]$ $1.php [NC,L]
8 và tất cả các điều này giải thích một phần một số ví dụ về Regex xấu hơn trên trang này.Tôi sẽ cung cấp thêm một cách tiếp cận, mà tôi nghĩ rằng dễ hiểu hơn. Tôi hy vọng điều này giúp người đọc trong tương lai hiểu mã mà họ sử dụng, thay vì chỉ sao chép và dán mã mà họ hầu như không hiểu và hy vọng điều tốt nhất.
RewriteEngine on
# Remove .html [or htm] from visible URL [permanent redirect]
RewriteCond %{REQUEST_URI} ^/[.+]\.html?$ [nocase]
RewriteRule ^ /%1 [L,R=301]
# Quietly point back to the HTML file [temporary/undefined redirect]:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]
Hãy cùng phá vỡ nó
Quy tắc đầu tiên khá đơn giản. Điều kiện phù hợp với bất kỳ URL nào kết thúc trong .html
[hoặc
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
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]
1] và chuyển hướng đến URL mà không có phần mở rộng tên tệp. Đó là một chuyển hướng vĩnh viễn để chỉ ra rằng URL bị cắt là chính thức.Quy tắc thứ hai cũng đơn giản. Điều kiện đầu tiên sẽ chỉ vượt qua nếu tên tệp được yêu cầu không phải là thư mục hợp lệ [
1]. Cái thứ hai sẽ chỉ vượt qua nếu tên tệp đề cập đến một tệp hợp lệ [
2] với tiện ích mở rộng .html được thêm vào. Nếu cả hai điều kiện đều vượt qua, quy tắc viết lại chỉ cần thêm ‘.html, vào tên tệp. Và sau đó phép thuật xảy ra trên
3. Đúng, đó là tất cả những gì cần thiết để ngăn chặn một vòng lặp vô hạn. Tài liệu của Apache Rewriterule Flags giải thích nó:Sử dụng cờ [end] chấm dứt không chỉ vòng xử lý viết lại hiện tại [như [l]] mà còn ngăn chặn mọi xử lý viết lại tiếp theo xảy ra trong bối cảnh mỗi hướng [HTACCess].