• 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 rewrite rule based query string on Ubuntu 22.04

  • 00:48 lsb_release -a
  • 01:02 cd /var/www/
  • 01:08 ls -la
  • 01:24 vim old.html
  • 01:47 vim new.html
  • 02:14 vim /etc/apache2/sites-available/000-default.conf
  • 04:28 a2enmod rewrite
  • 04:44 systemctl restart apache2
{{postValue.id}}

To Configure Apache Rewrite Rule Based Query String On Ubuntu 22.04

Introduction:

The Apache mod_rewrite module is a powerful and flexible tool for modifying URLs. It allows web administrators to adjust URLs based on various conditions, including query strings, which are the segments of a URL following the "?" character and are typically used for passing parameters to web applications.

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 : Go to the Apache document root directory

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

Step – 3 : Now list the directory

root@linuxhelp:/var/www# ls -la
total 12
drwxr-xr-x  3 root root 4096 Oct 29 04:06 .
drwxr-xr-x 15 root root 4096 Oct 29 03:57 ..
drwxr-xr-x  2 root root 4096 Oct 29 03:57 html

Step – 4 : Create a file using vim editor

root@linuxhelp:/var/www# vim old.html
Add the following text
OLD file

Step – 5 : Now create another file

root@linuxhelp:/var/www# vim new.html
Add the following text
NEW file

Step – 6 : Then make some changes in Apache default virtual host file

root@linuxhelp:/var/www# vim /etc/apache2/sites-available/000-default.conf
## Edit this line
DocumentRoot /var/www/
### Add this lines
	<Directory /var/www>
	RewriteEngine On
	RewriteCond %{QUERY_STRING} ^parameter1=value1$
	RewriteRule ^old\.html$ /new.html? [R=301,l]
	</Directory>

Step – 7 : Enable Apache rewrite module by using following command

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

Step – 8 : Restart the Apache2 service by using following command to apply new changes

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

Step – 9 : Now go to the browser and search with the following URL http://IP_ADDRESS/old.html?parameter1=value1 SNAP 1

Conclusion :

We have reached the end of this article. In this guide, we have walked you through the steps required to configure Apache rewrite rule based query string 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

What is the syntax for rewriting a URL with a query string using Apache RewriteRule?

A

The basic syntax is:
RewriteRule ^old-url$ new-url?q=query-value [R,L]
Where `^old-url$` matches the original URL, `new-url` is the rewritten URL, `q=query-value` adds a query string to
the end of the rewritten URL, and `[R,L]` specifies that this rule should be executed last.

Q

How do I add multiple query strings using Apache RewriteRule?

A

You can use the `&` character to separate multiple query strings. For example:
RewriteRule ^old-url$ new-url?q=param1&another-param=value [R,L]
This will result in a rewritten URL like `new-url?q=param1&another-param=value`.

Q

How do I preserve the original query string when rewriting a URL using Apache RewriteRule?

A

You can use the `%{QUERY_STRING}` variable to capture the original query string and then append it to the
rewritten URL. For example:
RewriteRule ^old-url$ new-url?q=query-value%{QUERY_STRING} [R,L]
This will preserve any existing query strings and append them to the end of the rewritten URL.

Q

Can I modify a query string using Apache RewriteRule?

A

Yes, you can use the `RewriteCond` directive to modify a query string. For example:
RewriteCond %{QUERY_STRING} ^param1=old-value$ [NC]
RewriteRule ^old-url$ new-url?q=param1=new-value [R,L]
This will rewrite any URLs that match the original URL and have `param1` set to `old-value`, changing it to
`new-value`.

Q

How do I handle special characters in query strings when using Apache RewriteRule?

A

When working with query strings, you may need to escape special characters like `&` or `=`. You can use the
`%{QUERY_STRING}` variable and URL-encode any special characters as needed. For example:
RewriteRule ^old-url$ new-url?q=param1=%25amp%3Bencoded-value [R,L]
This will result in a rewritten URL like `new-url?q=param1=&encoded-value`.

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 Gibbson ?
How do i run both nginx and apache in same instance on centos

Hi...,

my server is based centos operating system and my webserver is already running on Apache.... i need to run both apache and nginx on same instance ... please help me to implement this concept...

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.