.htaccess (hypertext access) is a hidden file used to configure additional features for websites hosted on Apache Web Server. With it, you can rewrite URL, password-protect directories, enable hotlink protection, disallow access to specific IP addresses, change your website’s time zone or alter default index page, and much more. Here you’ll learn how to locate and create .htaccess file.
###############################################
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /imranonline/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /imranonline/index.php [L]
</IfModule>
###############################################
## minified css / js base_url differences fixes
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
###############################################
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
<IfModule mod_expires.c>
#ExpiresActive On
#ExpiresDefault "access plus 1 month"
#ExpiresByType image/x-icon "access plus 1 year"
#ExpiresByType image/gif "access plus 1 month"
#ExpiresByType image/png "access plus 1 month"
#ExpiresByType image/jpg "access plus 1 month"
#ExpiresByType image/jpeg "access plus 1 month"
#ExpiresByType text/css "access plus 1 month"
#ExpiresByType text/x-javascript "access plus 1 month"
#ExpiresByType application/javascript "access plus 1 month"
#ExpiresByType application/x-javascript "access plus 1 month"
</IfModule>
######################################
## EXPIRES CACHING
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
######################################
# Not found 404 Page
ErrorDocument 404 http://www.haiuae.com/404
RewriteEngine On
RewriteCond %{HTTP_HOST} imranonline\.com [NC]
RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ https://www.imranonline.net/$1 [R,L]
RewriteRule ^(.*)$ https://localhost/imranonline$1 [R,L]
# php -- BEGIN cPanel-generated handler, do not edit
# This domain inherits the “PHP” package.
# php -- END cPanel-generated handler, do not edit
FAQs
1. What is an .htaccess file and where is it located?
Answer: An .htaccess (hypertext access) file is a hidden configuration file used on Apache web servers to manage various website settings. It’s typically located in the root directory of your website and allows for directory-level configuration without altering the main server settings.
2. How can I create or edit an .htaccess file?
Answer: To create or edit an .htaccess file, access your website’s root directory via FTP or a file manager provided by your hosting service. Ensure that your file manager displays hidden files, as .htaccess is hidden by default. You can create a new .htaccess file using a text editor or edit an existing one to configure your desired settings.
3. What are some common uses of the .htaccess file?
Answer: The .htaccess file is versatile and can be used for various purposes, including:
- URL rewriting
- Password-protecting directories
- Enabling hotlink protection
- Blocking specific IP addresses
- Changing the default index page
- Setting custom error pages
- Configuring MIME types
- Implementing caching policies
These configurations enhance website functionality, security, and performance.
4. How can I use .htaccess to redirect URLs?
Answer: To redirect URLs using .htaccess, you can employ the RewriteEngine
and RewriteRule
directives. For example, to redirect all traffic from http://example.com
to https://example.com
, you can add the following code to your .htaccess file:
apacheCopyEditRewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This configuration ensures that all HTTP requests are redirected to their HTTPS counterparts, enhancing security.
5. What precautions should I take when modifying the .htaccess file?
Answer: When editing the .htaccess file, it’s crucial to:
- Backup the existing file: Before making changes, save a copy of the current .htaccess file to prevent potential issues.
- Use correct syntax: Ensure that directives are correctly formatted to avoid server errors.
- Test changes thoroughly: After modifications, test your website to confirm that it’s functioning as intended.
- Limit access: Restrict access to the .htaccess file to prevent unauthorized alterations.
By following these precautions, you can safely manage your website’s configuration using the .htaccess file.
Implementing these practices will help you effectively utilize the .htaccess file to enhance your website’s performance and security.