Solved

How can I redirect to an anchor with mod_rewrite?

Posted on 2008-06-16
6
1,151 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
[X]
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
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

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…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
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…

751 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