Solved

How can I redirect to an anchor with mod_rewrite?

Posted on 2008-06-16
6
1,142 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
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]
0
 
LVL 27

Assisted Solution

by:caterham_www
caterham_www earned 250 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?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 21933071
> 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
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.

 
LVL 27

Expert Comment

by:caterham_www
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.
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
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]
0
 
LVL 3

Author Closing Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
Need Regex Money No $ 5 81
do u know a regex code to filter this?equal with lenght 32 6 37
AWS EC2 & RDS Instance 5 50
Create link to folder for use with apache. 7 31
As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
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…

829 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