• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to configure Apache redirect rules based file extensions on Debian 12

  • 00:46 cat /etc/os-release
  • 00:58 vim /etc/apache2/apache2.conf
  • 01:26 a2enmod rewrite
  • 01:46 cd /var/www/html/
  • 02:00 ls -la
  • 02:06 vim test.html
  • 02:31 systemctl restart apache2
  • 03:13 vim test.php
  • 03:43 vim .htaccess
  • 04:45 systemctl restart apache2
  • 05:19 vim .htaccess
  • 06:13 systemctl restart apache2
  • 06:40 vim .htaccess
  • 09:10 systemctl restart apache2
{{postValue.id}}

To Configure Apache Redirect Rules Based File Extensions On Debian 12

Introduction:

The Apache mod_rewrite module provides the capability to establish intricate URL redirects through the use of regular expressions. This functionality enables the matching of various file extensions, including but not limited to .html, .php, and .css. By utilizing these advanced redirect rules, you can enhance your website's URL management, thereby facilitating improved navigation for both users and search engines.

Procedure :

Step 1: Check the OS version by using following command.

root@linuxhelp:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL=https://bugs.debian.org/

Step 2: Open Apache configuration file by using following command.

root@linuxhelp:~# vim /etc/apache2/apache2.conf
<Directory /var/www/>
	Options Indexes FollowSymLinks
	AllowOverride All
	Require all granted
</Directory>

Step 3: Enable the Apache rewrite module by using following command.

root@linuxhelp:~# a2enmod rewrite
Module rewrite already enabled

Step 4: Go to the Apache document root directory location by using following command.

root@linuxhelp:~# cd /var/www/html/

Step 5: Longlist the files by using following command.

root@linuxhelp:/var/www/html# ll
total 4
-rw-r--r-- 1 root root 44 Aug 23 16:18 index.html

Step 6: Create a HTML file by using following command.

root@linuxhelp:/var/www/html# vim test.html
testing html file

Step 7: Restart the Apache service by using following command.

root@linuxhelp:/var/www/html# systemctl restart apache2

Step 8: Open firefox browser and search IP address with .html extension with file name. snap 1

Step 9: Create a PHP file by using following command.

root@linuxhelp:/var/www/html# vim test.php
testing php file

Step 10: Create .htaccess file for make apache rewrite rules by using following command.

root@linuxhelp:/var/www/html# vim .htaccess
RewriteEngine On
RewriteRule ^test.html$

Step 11: Open Firefox browser and refresh the page. snap 2

Step 12: Open .htaccess file and modify rewrite rule to redirect .html to .php file content.

root@linuxhelp:/var/www/html# vim .htaccess
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]

Step 13: Restart the Apache service by using following command.

root@linuxhelp:/var/www/html# systemctl restart apache2

Step 14: Open Firefox browser and refresh the page. snap 3

Step 15: Open .htaccess file and modify rewrite rule to redirect file name to .php file extension.

root@linuxhelp:/var/www/html# vim .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]

Step 16: Restart the Apache service by using following command.

root@linuxhelp:/var/www/html# systemctl restart apache2

Step 17: Open Firefox browser and search file name only without extension. snap 4

Conclusion :

We have reached the end of this article. In this guide, we have walked you through the steps required to configure Apache redirect rules based file extension on Debian 12. Your feedback is much welcome.

Tags:
matthew
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is mod_rewrite, and how does it work?

A

Mod_rewrite is an Apache module that allows you to create URL redirects using regular expressions. It works by examining incoming requests against custom rules, which can be used to rewrite or redirect URLs.

Q

Why use file extension-based redirects in Apache?

A

Using file extension-based redirects in Apache can help improve website performance and user experience by allowing you to optimize URL mappings and handle specific file types more efficiently.

Q

Can I use mod_rewrite with other Apache modules, like mod_alias or mod_proxy?

A

Yes, mod_rewrite can be used in conjunction with other Apache modules, including mod_alias and mod_proxy. However, when using multiple modules together, it's essential to ensure that each module is properly configured and doesn't conflict with the others.

Q

How do I test my Apache redirect rules?

A

To test your Apache redirect rules, you can use tools like curl or a web browser to simulate incoming requests against your website. You should also verify that the redirects are working as expected by checking the server logs and ensuring that the correct files are being accessed.

Q

Can I use file extension-based redirects with other web servers besides Apache?

A

While mod_rewrite is specific to Apache, other web servers like Nginx or Lighttpd have similar modules (e.g., rewrite in Nginx) that can be used for URL rewriting and redirecting. However, the specific configuration and syntax may differ from Apache.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Jayce ?
What are the types of table used in IPtables

What are the various types of table used in IPtables and how to use that for my server security?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.