Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How can I redirect to an anchor with mod_rewrite?

Posted on 2008-06-16
Medium Priority
Last Modified: 2010-04-21
Hi all,

We're currently working on a website that has rewritten URLs all over the place - they all work ok at the moment.  I'm trying to add another one that redirects to an HTML anchor.  The code snippet attached to this post is the rule as it stands now (it doesn't work).  For example, the full page URL might be something like this (works):


There is an anchor in the file that is specified as <a name="post_comments">Some PHP here</a>

What I want mod_rewrite to do is accept a URL like this:


and rewrite it to:


The mod_rewrite documentation says to use the [NE] flag so that default behaviour of escaping the # character doesn't happen.  I've also found some pages online that have Javascript in them to detect if there's a # in the URL and if so, redirect (doesn't work for me).

Can someone help?  #post_comments on the end of the URLs is uuuuuugly and I'd like them to say /comments instead.  Feel free to rip apart the rule attached in the code snippet - it's just the results of me playing about with various combinations and is probably completely wrong.

Regular expressions still mystify me a bit so please bear with me on that ...

Thanks in advance!
# If we have 4 values and the last is the word 'comments' - rewrite to blog's comments
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/(comments)/$ /index2.php?group=$1&trip=$2&blog=$3#post_comments [NC,NE,L]

Open in new window

Question by:Number5ix
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 51

Accepted Solution

ahoffmann earned 750 total points
ID: 21803791
if following does not work, please post corresponding messages from your error_log

RewriteCond %{REQUEST_URI} !^/*index2\.php
RewriteRule ^/+([^/]+)/([^/]+)/([^/]+)/(comments)/*$ /index2.php?group=$1&trip=$2&blog=$3#post_comments [NC,NE,L]
LVL 27

Assisted Solution

caterham_www earned 750 total points
ID: 21932994
>> /hello/there/dude/comments

and rewrite it to:

index2.php?group=hello&trip=there&blog=dude#post_comments <<

That doesn't work (logically, unless you redirect to index2.php). This is a server-internal rewrite. Redirect /hello/there/dude/comments/ --> /hello/there/dude/comments/#post_comments first.

RewriteRule ^([^/]+/[^/]+/[^/]+/comments/)$ /$1#post_comments [NC,NE,R=301,L]

>  #post_comments on the end of the URLs is uuuuuugly and I'd like them to say /comments instead.

Not possible. This is a browser issue, this cannot be done internally with mod_rewrite. How should the browser know to move forward to #post_comments without telling him to do so?
LVL 51

Expert Comment

ID: 21933071
> Not possible.
There is no problem to send a url with an fragment from the server to the browser, try yourself with a Location header.
(don't be confused that the browser never sends the fragment in a request to the server)
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 27

Expert Comment

ID: 21934016
> send a url with an fragment from the server to the browser

correct. But
RewriteRule ^/+([^/]+)/([^/]+)/([^/]+)/(comments)/*$ /index2.php?group=$1&trip=$2&blog=$3#post_comments [NC,NE,L]
does not send anything to the browser - this is just an internal rewrite rule.

> (don't be confused that the browser never sends the fragment in a request to the server)

I'm aware of that.

> Not possible.
The OP wants to hide the fragment in the browser's location bar, so it shouldn't be shown at all. How should this be possible? If you "send a url with an fragment from the server to the browser" (also known as [external] redirect[ion]) it will become visible in the browser's location bar - but the OP dosen't want that to be visible.
LVL 51

Expert Comment

ID: 21937792
> .. wants to hide the fragment in the browser's location bar,
hmm, don't see this requirement written in the question,
anyway I missed th R flag in my suggestion.
Thanks caterham_www for pointing that out.

RewriteCond %{REQUEST_URI} !^/*index2\.php
RewriteRule ^/+([^/]+)/([^/]+)/([^/]+)/(comments)/*$ /index2.php?group=$1&trip=$2&blog=$3#post_comments [NC,NE,L,R]

Author Closing Comment

ID: 31467531
Hi all, none of the specific answers above worked 100% but I learnt a lot from the info and solved some other questions with the info provided.  Hope you don't mind sharing the points ...

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question