Apache Rewrite rules for handling URLs containing "#" | Community
Skip to main content
Rohan_Garg
Community Advisor
Community Advisor
March 20, 2024
Solved

Apache Rewrite rules for handling URLs containing "#"

  • March 20, 2024
  • 2 replies
  • 3805 views

Hi All,

 

I know this is a question more suited for other forums but just wanted to get an opinion if anyone has a handy rewrite rule for the below use case - 

I want to redirect a URL for the format "https://abc.com/x/y#z" to "https://abc.com/"

 

The typical rewrite rule which we use is as below - 

RewriteRule ^/x/y  / [R=301,L,NE]

This however yields the URL - https://abc.com/#z


Changing the rule to below (including # in Rewrite rule) leads to the rule not being met at all -

RewriteRule ^/x/y#z  / [R=301,L,NE]

 

Does anyone have a handy regex rule for handling the above use case?

@imran__khan, @arunpatidar, @sureshdhulipudi@raja_reddy ,@aanchal-sikka@estebanbustamante 

 

Thanks in advance,

Rohan Garg

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

Hey @arunpatidar, sorry for using redirect and rewrite interchangeably! 
I understand that the /#randomString to / has to happen at browser and not webserver.

But the problem is the page in question has lot of valid sections as well - so we don't want to simply strip off #randomString - The only way I see is we fetch all sections in the page and then check if the rewrite hash is valid or not. 
Let me know if this approach and understanding is incorrect!


yes, you need to create a config in AEM and expose as json and based on entry, perform client side redirect.

2 replies

arunpatidar
Community Advisor
Community Advisor
March 20, 2024

"the anchor tag is never sent as part of the HTTP request by any browser. It is only interpreted locally within the browser".

The new rule would be:

RewriteRule ^/x/y$  / [R=301,L,NE]

 

Arun Patidar
Rohan_Garg
Community Advisor
Community Advisor
March 20, 2024

Thanks for the prompt reply @arunpatidar! Currently we are looking at the URLs which might be bookmarked which often includes the anchored part as well.
However, if you run the above redirect on the htaccess tester you will get the below result (https://htaccess.madewithlove.com?share=0e704428-323e-461d-9cfe-34bc3b5f6df6)

 

The URL bounces in endless redirection from https://www.abc.com/#z to https://www.abc.com/%23z

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
March 20, 2024

Hey @arunpatidar, sorry for using redirect and rewrite interchangeably! 
I understand that the /#randomString to / has to happen at browser and not webserver.

But the problem is the page in question has lot of valid sections as well - so we don't want to simply strip off #randomString - The only way I see is we fetch all sections in the page and then check if the rewrite hash is valid or not. 
Let me know if this approach and understanding is incorrect!


yes, you need to create a config in AEM and expose as json and based on entry, perform client side redirect.

Arun Patidar
kautuk_sahni
Community Manager
Community Manager
March 20, 2024

@rohan_garg 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
Rohan_Garg
Community Advisor
Community Advisor
March 20, 2024

Hey @kautuk_sahni, I will be sure to mark this query resolved once done! Thanks!