Dispatcher Rewrite | Community
Skip to main content
Level 2
March 6, 2024

Dispatcher Rewrite

  • March 6, 2024
  • 6 replies
  • 2641 views

Can somebody help me to redirect dispatcher url for below need?

 

https://localhost:8080/content/mysite/home/ca/en/global/search-results.html?rootpath=/ca/en/abc&param=test

 

to 

 

https://localhost:8080/ca/en/abc/test (remove Group 1 - /content/mysite/home/ca/en/global/search-results, .html. ?, rootpath= Group 2 - /ca/en/abc&param)

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

6 replies

Raja_Reddy
Community Advisor
Community Advisor
March 7, 2024

Hi @gatsby 

1.Enable the mod_rewrite module in Apache.

2.Add the following rewrite rule to your Apache configuration file:

RewriteEngine On

RewriteRule ^/ca/en/global/search-results/rootpath=(.*)$ /content/mysite/home/ca/en/global/search-results.html?rootpath=$1 [L]

This rule will match any URL that starts with /ca/en/global/search-results/rootpath= and capture the value of rootpath as a group. It will then rewrite the URL to the original URL with the rootpath parameter set to the captured value.

3.Restart Apache to apply the changes.

Lokesh_Vajrala
Community Advisor
Community Advisor
March 7, 2024

 

## check if there is a rootpath query param with request
RewriteCond %{QUERY_STRING} ^rootpath=(.*)$
## rewrite the path with 302 redirect. If you don't want user to see the redirect, change R=302 with PT RewriteRule ^/content/mysite/home/ca/en/global/search-results\.html$ /ca/en/global/search-results/rootpath/%1 [R=302,L] ## %1 captures the value of the query string. If you don't need it just replace it with "test" as per your usecase. 

 

Thanks,

Lokesh 

GatsbyAuthor
Level 2
March 7, 2024

Lokesh_Vajrala
Community Advisor
Community Advisor
March 8, 2024

@gatsby  Is not the URL path not matching with the "/content/mysite/home/ca/en/global/search-results.html"?

Please check the Request URL path and update the RewriteRule to match with the path. 

 

GatsbyAuthor
Level 2
March 7, 2024

Some how I can able to achieve the part of my requirement as below:

RewriteCond %{QUERY_STRING} ^rootpath=(.*)&param=(.*) [NC]
RewriteRule ^(.*) %1/%2 [R302,L]

 
AMANATH_ULLAH
Community Advisor
Community Advisor
March 7, 2024

@gatsby 

You can use QSD flag in rewrite rule to achieve this

https://httpd.apache.org/docs/trunk/rewrite/flags.html#flag_qsd

Amanath Ullah
arunpatidar
Community Advisor
Community Advisor
March 7, 2024

Hi @gatsby 
You can try with below

 

RewriteCond %{REQUEST_URI} ^/content/mysite/home/ca/en/global/search-results\.html$ RewriteCond %{QUERY_STRING} ^rootpath=([^&]+)&param=(.*)$ RewriteRule ^/content/mysite/home/ca/en/global/search-results\.html$ /%1/%2? [R=301,L]

 

Test Link

Arun Patidar
GatsbyAuthor
Level 2
March 7, 2024

@arunpatidar @amanath_ullah There is 404 after using below rule

RewriteCond %{QUERY_STRING} ^rootpath=([^&]+)&param=(.*)$
RewriteRule ^(.*) %1/%2 [R=302,L,QSD]

kautuk_sahni
Community Manager
Community Manager
March 7, 2024

@gatsby Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni
kautuk_sahni
Community Manager
Community Manager
March 14, 2024

@gatsby  If you have found out solution yourself, please share it with the community.

Kautuk Sahni