19

I want to rewrite http://example.com/articles.html#first-article with http://example.com/articles/first-article

Is it possible to rewrite?

I tried using following but didn't work for me:

RewriteRule ^articles/(.+)$ /articles\.html\#$1
6
  • 2
    As @fab answered, the fragment is not sent to the server. You could setup a catch-all page and use JavaScript to do the redirecting.
    – Bouke
    Feb 28, 2013 at 10:31
  • 1
    @Bouke: WRONG/ Read the Apache documentation, please.
    – Bo Reth
    Jan 16, 2020 at 20:35
  • 1
    @BoReth it doesn't have anything to do with Apache actually. See also stackoverflow.com/questions/3067491/…. To reiterate; the server doesn't have access to the anchor, and therefore it cannot use the anchor in rewrite rules.
    – Bouke
    Jan 17, 2020 at 15:09
  • 1
    @Bouke: Well, it has everything to do with Apache because it is about REDIRECTING. If there is a rewrite rule inside "httpd.conf" or ".htacces" with a RewriteRule . /Dir1/Dir2/#ApacheAnchor [NE,R=301,L], for example, the SERVER will indeed redirect to that URL and the RESPONSE HEADER will contain something like: "Location:mydomain/Dir1/Dir2/#ApacheAnchor". Therefore, claiming that the ANCHOR or the fragment are NEVER sent to the server is not accurate, in my opinion.
    – Bo Reth
    Jan 17, 2020 at 16:22
  • 2
    @BoReth you’re confusing sending data to the server with receiving data from the server. The question is regarding basing a server-side redirect on headers received from the client.
    – Bouke
    Jan 18, 2020 at 18:14

2 Answers 2

45

The # can be added in the substitution URL with the NE flag. Check:

So, you may try this:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !articles\.html      [NC]
RewriteCond %{REQUEST_URI}  ^/articles/([^/]+)/? [NC]
RewriteRule .*       /articles.html#%1           [R,NE,L]

Redirects

http://example.com/articles/parameter

To

http://example.com/articles.html#parameter

String articles is assumed to be fixed while parameter is assumed to be variable.

4
  • 22
    Redirecting to an anchor was not the question. Feb 28, 2013 at 13:58
  • 12
    This doesn't answers this question but is useful
    – zardilior
    Jul 8, 2015 at 20:49
  • Similar problem here I can't solve. stackoverflow.com/questions/31859377/… Aug 6, 2015 at 15:11
  • 2
    Redirecting to an anchor is the question. The title is: 'hash' url rewrite in .htaccess, where HASH means ANCHOR. The example in the question is: RewriteRule ^articles/(.+)$ /articles\.html\#$1. Since there is an ANCHOR (#) in the target URL of the example, the question is clearly asking for a way to redirect to an ANCHOR. Now, this answer is important because it clarifies the common misconception that the ANCHOR never "...gets sent to the server", contrary to Apache documentation.
    – Bo Reth
    Jan 17, 2020 at 7:06
20

No, it's not possible. The URL fragment (everything from # on) not even gets sent to the server.

Maybe you want to consider a JavaScript based solution, where the actual content will be loaded via AJAX dependent on the fragment. You will find some useful information about this method here: What's the shebang/hashbang (#!) in Facebook and new Twitter URLs for?

5
  • 7
    This should be de-selected as the correct answer as it is incorrect. @faa's answer below is in fact correct. Aug 28, 2013 at 10:19
  • 10
    Depends on the question. The answer of @faa is correct but it does not address the issue of the original poster. Sep 10, 2013 at 9:04
  • 2
    Good point, I was a little tired when I read it all first time, my bad. Sep 10, 2013 at 16:15
  • 2
    Question asks for a way to remove the "#" character (Called ANCHOR by Apache). You say "No, it's not possible" and "Redirecting to an anchor was not the question". Then, why do you answer it in the 1st line? You also say: "The URL fragment (everything from # on) not even gets sent to the server." Completely WRONG, in my opinion. The "URL fragment" is indeed sent when NOT escaped, as explained in Apache documentation. I have tried it myself and unless Apache is wrong and you are right, your statement is NOT accurate. @Alameda answer does indeed answer the question, IMHO.
    – PDR
    Jan 9, 2020 at 2:53
  • 1
    This not an answer. It is just a wrong suggestion, but it has a lot of upvotes (?). In fact, this statement is wrong: "No, it's not possible. The URL fragment (everything from # on) not even gets sent to the server." Of course it is possible. Read the Apache documentation, please. I agree with @designermonkey. Can't downvote, for now, but I will when I have the required reputation.
    – Bo Reth
    Jan 16, 2020 at 20:30

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.