Ashar
Ashar

Reputation: 3245

special character # in URL does not work with RewriteRule apache httpd

I'm able to redirect

https://my.bank.com/caweb/ptbmc/appInfo

to the maintenance page

https://my.bank.com/maintain.html

cat httpd-ssl.conf

RewriteEngine on
RewriteCond %{QUERY_STRING} /caweb/#/ptbmc
RewriteRule (.*) https://my.bank.com/
RewriteRule ^(/caweb/ptbmc.*)$ /maintain.html [PT]

However, i need to redirect the URL now containing the special character # i.e., https://my.bank.com/caweb/#/ptbmc/appInfo

to the maintenance page

https://my.bank.com/maintain.html

I tried changing RewriteRule to -> RewriteRule ^(/caweb/#/ptbmc.*)$ /maintain.html [NE] but it did not work as well.

Can you please suggest what changes do i have to make in the RewriteRule to get this to work?

Upvotes: 0

Views: 508

Answers (1)

Olaf Kock
Olaf Kock

Reputation: 48057

From Wikipedia (emphasis mine):

In URIs, a hash mark # introduces the optional fragment near the end of the URL. The generic RFC 3986 syntax for URIs also allows an optional query part introduced by a question mark ?. In URIs with a query and a fragment, the fragment follows the query. Query parts depend on the URI scheme and are evaluated by the server—e.g., http: supports queries unlike ftp:. Fragments depend on the document MIME type and are evaluated by the client (web browser). Clients are not supposed to send URI fragments to servers when they retrieve a document, and without help from a local application (see below) fragments do not participate in HTTP redirections.

So, as I said in the comment: You can't handle a # sign server side as the condition for a redirect, because the server never sees it. The browser is supposed to not send it.

In this case, if you need any redirect, you'd either need to do it on the browser side (e.g. in Javascript), rewrite the HTML that's generated in the first place, or generate different URLs that don't rely on #.

Upvotes: 1

Related Questions