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
The #
can be added in the substitution URL with the NE flag. Check:
This Apache link that describes specifically how to redirect anchors
.
Another answer in SO (mine).
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.
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.
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?
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.