Solved

How can I redirect to an anchor with mod_rewrite?

Posted on 2008-06-16
6
1,128 Views
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):

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

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:

/hello/there/dude/comments

and rewrite it to:

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

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

0
Comment
Question by:Number5ix
  • 3
  • 2
6 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 250 total points
Comment Utility
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]
0
 
LVL 27

Assisted Solution

by:caterham_www
caterham_www earned 250 total points
Comment Utility
>> /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?
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> Not possible.
disagreed
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)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 27

Expert Comment

by:caterham_www
Comment Utility
> 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.
disagreed
<<
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> .. 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]
0
 
LVL 3

Author Closing Comment

by:Number5ix
Comment Utility
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 ...
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now