Skip to main content
Level 2
Mar 6, 2024 11:56 PM UTC

Dispatcher Rewrite

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.

10 replies

Raja_Reddy
Community Advisor
Community Advisor
Mar 7, 2024 12:53 AM UTC

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.

Translate
Lokesh_Vajrala
Community Advisor
Community Advisor
Mar 7, 2024 4:27 AM UTC

 

## 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 

Translate
GatsbyAuthor
Level 2
Mar 7, 2024 4:31 AM UTC

Translate
Lokesh_Vajrala
Community Advisor
Community Advisor
Mar 8, 2024 4:48 AM UTC

@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. 

 

Translate
GatsbyAuthor
Level 2
Mar 7, 2024 6:15 AM UTC

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]

 
Translate
AMANATH_ULLAH
Community Advisor
Community Advisor
Mar 7, 2024 8:25 AM UTC

@gatsby 

You can use QSD flag in rewrite rule to achieve this

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

Translate
Amanath Ullah
arunpatidar
Community Advisor
Community Advisor
Mar 7, 2024 10:04 AM UTC

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

Translate
Arun Patidar
GatsbyAuthor
Level 2
Mar 7, 2024 4:12 PM UTC

@arunpatidar @amanath_ullah There is 404 after using below rule

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

Translate
kautuk_sahni
Level 9
Mar 7, 2024 2:13 PM UTC

@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.

Translate
Kautuk Sahni
kautuk_sahni
Level 9
Mar 14, 2024 8:27 AM UTC

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

Translate
Kautuk Sahni