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
blue ghhgdtt
  • 907
  • 4
  • 11
  • 16
  • 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 '13 at 10:31
  • 1
    @Bouke: WRONG/ Read the [Apache documentation](http://httpd.apache.org/docs/2.2/rewrite/advanced.html#redirectanchors), please. – Bo Reth Jan 16 '20 at 20:35
  • 1
    @BoReth it doesn't have anything to do with Apache actually. See also https://stackoverflow.com/questions/3067491/is-the-anchor-part-of-a-url-being-sent-to-a-web-server. To reiterate; the server doesn't have access to the anchor, and therefore it cannot use the anchor in rewrite rules. – Bouke Jan 17 '20 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:http://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 '20 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 '20 at 18:14
  • 1
    Gosh that's confusing. In your question you have the # in your source string and in your example you've put the # in the target string. No wonder there are some mixed up answers below. – clayRay Jul 14 '21 at 09:12

2 Answers2

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.

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • 22
    Redirecting **to** an anchor was not the question. – Fabian Schmengler Feb 28 '13 at 13:58
  • 12
    This doesn't answers this question but is useful – zardilior Jul 08 '15 at 20:49
  • Similar problem here I can't solve. http://stackoverflow.com/questions/31859377/why-does-this-htaccess-rewriterule-that-includes-a-fail – user1452893 Aug 06 '15 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](http://httpd.apache.org/docs/2.2/rewrite/advanced.html#redirectanchors). – Bo Reth Jan 17 '20 at 07: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?

Community
  • 1
  • 1
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
  • 7
    This should be de-selected as the correct answer as it is incorrect. @faa's answer below is in fact correct. – designermonkey Aug 28 '13 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. – Fabian Schmengler Sep 10 '13 at 09:04
  • 2
    Good point, I was a little tired when I read it all first time, my bad. – designermonkey Sep 10 '13 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 09 '20 at 02: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 '20 at 20:30