• 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 Ubuntu 22.04

  • 01:01 lsb_release -a
  • 01:14 systemctl status apache2
  • 01:32 vim /etc/apache2/apache2.conf
  • 02:05 a2enmod rewrite
  • 02:18 cd /var/www/html/
  • 02:30 vim test.html
  • 02:52 systemctl restart apache2
  • 03:32 vim test.php
  • 03:59 vim .htaccess
  • 05:10 systemctl restart apache2
  • 05:39 vim .htaccess
  • 06:29 systemctl restart apache2
  • 06:51 vim .htaccess
  • 08:48 systemctl restart apache2
{{postValue.id}}

To Configure Apache Redirect Rules Based File Extensions On Ubuntu 22.04

Introduction:

The Apache mod_rewrite module allows for the clean rewriting of URLs, converting user-friendly paths into code-compatible query strings. Additionally, it provides the capability to rewrite URLs based on specific conditions. An .htaccess file facilitates the creation and implementation of rewrite rules without requiring access to server configuration files. By placing the .htaccess file in the root directory of your website, you can manage rewrites on an individual site or directory basis.

Procedure:

Step 1: Check the OS version

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy

Step 2: Then check the status of the Apache2 service

root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2024-09-09 02:37:00 IST; 42s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 3672 (apache2)
      Tasks: 55 (limit: 4556)
     Memory: 5.3M
        CPU: 60ms
     CGroup: /system.slice/apache2.service
             ├─3672 /usr/sbin/apache2 -k start
             ├─3673 /usr/sbin/apache2 -k start
             └─3674 /usr/sbin/apache2 -k start
Sep 09 02:37:00 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Sep 09 02:37:00 linuxhelp apachectl[3661]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. >
Sep 09 02:37:00 linuxhelp systemd[1]: Started The Apache HTTP Server.

Step –3: Edit Apache configuration file by using vim editor

root@linuxhelp:~# vim /etc/apache2/apache2.conf 
change the following line
AllowOverride All

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

root@linuxhelp:~# a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2

Step 5: Change the directory to Apache document root directory

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

Step 6: Now create a simple HTML file using vim editor

root@linuxhelp:/var/www/html# vim test.html
HTML File

Step 7: Now restart the Apache service by using following command

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

Step 8: Open browser and search with the http://IP-Address/Filename. Snap1

Step 9: Again go to the terminal create simple PHP file using vim editor

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

Step 10: Create .htaccess file for Apache rewrite rules by using vim editor.

root@linuxhelp:/var/www/html# vim .htaccess
Add the following lines
RewriteEngine On
RewriteRule ^test.html$ test.php [L]

Step 11: Restart Apache service by using following command to apply changes.

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

Step 12: Now again open the browser and refresh the page Snap2

Step 13: 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 14: Restart the Apache service by using following command.

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

Step 15: Now again open the browser and refresh the page Snap3

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

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

Step 17: Restart the Apache service by using following command to apply changes

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

Step 18: Now again open the browser and remove the file extension then load the page Snap4

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to installation of Drupal CMS on Ubuntu 22.04. Your feedback is much welcome.

Tags:
matthew
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

How can I set up a redirect for all .html files to .php files in Apache?

A

To redirect all .html files to their .php counterparts, you can use a .htaccess file with the following rewrite rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)\.html$ $1.php [L]
This configuration checks if the .php version of the requested file exists and redirects to it if so.

Q

How can I redirect requests for .old files to a new .new file?

A

To redirect requests from .old files to .new files, you can use the following rule:
RewriteEngine On
RewriteRule ^(.*)\.old$ /$1.new [L,R=301]
This rule will perform a permanent (301) redirect from any .old file to its corresponding .new file.

Q

Is it possible to redirect requests based on multiple file extensions at once?

A

Yes, you can use a single rule to handle multiple file extensions. For example, to redirect .jpg, .jpeg, and .png files to a specific page, you can use:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|png)$
RewriteRule ^ /newpage.html [L,R=302]
This rule will redirect requests for any of these image formats to newpage.html.

Q

How do I set up a redirect for files that do not exist to a custom 404 error page?

A

To redirect users to a custom 404 error page when they request a non-existent file, you can use:
ErrorDocument 404 /404.html
This directive will show 404.html as the custom error page whenever a file is not found.

Q

Can I redirect requests based on file extensions and keep query parameters intact?

A

Yes, you can keep query parameters intact while redirecting based on file extensions. For example, if you want to redirect .html files to .php files but preserve any query strings, use:
RsssewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)\.html$ /$1.php [L]
This rule ensures that query parameters are preserved during the redirection. The query string will be appended to the new URL automatically.

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 David Lopez Guillen ?
Ayuda urgente instale SSL para servidor Opensuse y ahora no funciona tengo servicio web

hola segui este tutorial para tener un certificado ssl y ahora no se ve mi app en la red, espero alguien pueda ayudarme, tengo M9oodle en3.5 en un servidor open suse y ahora no funciona por favor ayuda.

https://www.linuxhelp.com/how-to-create-ssl-certificate-in-opensuse

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.