• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1187
  • Last Modified:

How can I redirect to an anchor with mod_rewrite?

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
Number5ix
Asked:
Number5ix
  • 3
  • 2
2 Solutions
 
ahoffmannCommented:
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
 
caterham_wwwCommented:
>> /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
 
ahoffmannCommented:
> 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
caterham_wwwCommented:
> 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
 
ahoffmannCommented:
> .. 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
 
Number5ixAuthor Commented:
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

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now