Expand my Community achievements bar.

Applications for the 2024-2025 Adobe Experience Manager Champion Program are open!

Apache Rewrite rules for old bookmarked URLs

Avatar

Level 4

Hi Team,

 

I have a requirement where I need to redirect old domain URLs to new domain.

 

Old URL patterenhttps://live.test.com/%23document%2F1533623%3Fpref%3D20058%2F9%2F5%26crumb%3D104

 

When user access above link then I need to redirect above old URL to new URL mentioned below

 

New URLhttps://prod.test.net/document?page=1533623&id=58

 

Note: 

  1. Old URI is in encoded format. will apache auto decodes this?
  2. The number in old URL 1533623 is the value for query param page in the new URL.
  3. The number in old URL 20058 needs to be converted to 58 in the new URL as a value of param id.

Could you please share apache mod rewrite rules for this requirement?

 

@kautuk_sahni @aanchal-sikka @lukasz-m 

 

Thanks.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

5 Replies

Avatar

Community Advisor

Hi,

 

I encourage you to learn and experiment with rewrite rules on your own in a local environment. You have some resources where you can learn more about this topic:

As part of what you are looking for, you can try something like the example below (which I have not tested but it may work):

# Rule to match the old URL pattern and redirect to the new URL
RewriteCond %{HTTP_HOST} ^live\.test\.com$ [NC]
RewriteCond %{QUERY_STRING} ^pref=(\d+)/(\d+)/(\d+)&crumb=(\d+)$
RewriteRule ^%23document/(\d+)$ https://prod.test.net/document?page=%1&id=%4 [R=301,L]

Explanation:

  1. RewriteCond %{HTTP_HOST} ^live\.test\.com$ [NC]: This condition checks if the request is coming to the old domain.

  2. RewriteCond %{QUERY_STRING} ^pref=(\d+)/(\d+)/(\d+)&crumb=(\d+)$: This condition captures the values you need from the old query string.

  3. RewriteRule ^%23document/(\d+)$ https://prod.test.net/document?page=%1&id=%4 [R=301,L]: This rule matches the old URL pattern and redirects to the new URL with the captured values.

    • %1 refers to the first captured group in the last RewriteCond, which is the value for the pref parameter (20058).
    • %4 refers to the fourth captured group, which is the value for the crumb parameter (104).
    • (\d+) captures the numeric value in the old URL, which is used as the value for the page parameter in the new URL.

The [R=301,L] flag indicates a permanent (301) redirect and that this is the last rule to be applied.

 

Hope this helps.



Esteban Bustamante

Avatar

Community Advisor

Hi @Uppari_Ramesh 

you should need to do however is include this in the .htaccess of your old domain:

Options +FollowSymLinks

RewriteEngine on

RewriteRule (.*) http://new-domain.com/$1 [R=301,L]

For specific directories:

 

Redirect 301 /mydir http://www.new-domain.com

or if it is to go to another directory:

 

Redirect 301 /mydir http://www.new-domain.com/mydir

 

set of mod_rewrite rules for your requirement:

apache

Copy code

<IfModule mod_rewrite.c>

  RewriteEngine On

  RewriteBase /

 

  # Check if the request is for the old domain and URL pattern

  RewriteCond %{HTTP_HOST} ^live\.test\.com$ [NC]

  RewriteCond %{REQUEST_URI} ^/[^?]*%23document%2F(\d+)%3Fpref%3D(\d+)%2F(\d+)%2F(\d+)%26crumb%3D(\d+)$

  

  # Redirect to the new domain and URL

  RewriteRule ^ https://prod.test.net/document?page=%1&id=%4 [R=301,L]

</IfModule>

Explanation of the rules:

The RewriteCond directives check if the request is coming from the old domain and matches the specified old URL pattern.

The regular expression in RewriteCond extracts the necessary values from the old URL.

The RewriteRule then performs the redirection to the new domain and constructs the new URL with the extracted values.

Make sure to place these rules in your Apache configuration file or in the .htaccess file in the document root of your old domain.

 

Note: The %1, %4, etc., in the RewriteRule pattern refer to the back-references from the RewriteCond regular expression. The values extracted from the old URL are used in constructing the new URL. The R=301 flag indicates a permanent.



Avatar

Administrator

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

Avatar

Level 4

Hi @EstebanBustamante  @Raja_Reddy , @kautuk_sahni 

 

I am following your suggestions and I had to modify the rules a bit according to my requirement. Below is the updated configuration

RewriteCond %{HTTP_HOST} ^live\.test\.com$ [NC]
RewriteMap map1 "txt:conf/rewritemaps/subtractionMap.txt"
RewriteMap map2 "txt:conf/rewritemaps/idMap.txt"
RewriteCond "%{QUERY_STRING}" "^pref=(\d+)/(\d+)/(\d+)&crumb=(\d+)$"
RewriteRule "^/#document/(\d+)" "https://prod.test.net/document?page=$1&id=${map1:%1}${map2:%2}" [R=301,L]

 

Explanation: I want to convert 20058 to 58 hence I have created a RewriteMap and given a identifier as map1. Also I need to convert %2 variable into text so I have created map2.

 

Also if I write RewriteRule as ^/%23document it is not working instead we should use as ^/#document. It seems apache auto decodes encoded URL.

 

If I hit the resource as /%23document/1533623?pref=20058/9/5&crumb=104 then only my rewrite rule is working.[only # encoded]

 

If I hit the resource as %23document%2F1533623%3Fpref%3D20058%2F9%2F5%26crumb%3D104

my rewrite is not working[[full encoding]] @EstebanBustamante  you have any idea on this?

 

Thanks.

Avatar

Community Advisor

Hi @Uppari_Ramesh 

  • You've mentioned that your rewrite rule works when the URL is encoded as /...?pref=20058/9/5&crumb=104 but not when fully encoded as %2F%23document%2F1533623%3Fpref%3D20058%2F9%2F5%26crumb%3D104.
  • This might be due to the fact that your RewriteCond is looking for a specific format in the query string, and when fully encoded, it may not match the pattern you've specified.
  • To address this, you might need to update your RewriteCond to handle fully encoded query strings. You can modify it

 

 

 

RewriteCond %{HTTP_HOST} ^live\.test\.com$ [NC]
RewriteMap map1 "txt:conf/rewritemaps/subtractionMap.txt"
RewriteMap map2 "txt:conf/rewritemaps/idMap.txt"
RewriteCond "%{QUERY_STRING}" "^pref=(\d+)\/(\d+)\/(\d+)&crumb=(\d+)$"
RewriteRule "^/#document/(\d+)" "https://prod.test.net/document?page=$1&id=${map1:%1}${map2:%2}" [R=301,L]
​

 

 



or
You can try 

RewriteRule "^/#document/(\d+)" "https://prod.test.net/document?page=$1&id=${map1:%1}${map2:%2}" [R=301,L,NE]