Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

bart van de ven
PLUS
bart van de ven
Courses Plus Student 8,214 Points

htaccess in combination with html anchor

I have been working with a htaccess file to process a comment box structure with the ability to reply to existing comments. I want to be able to redirect users that filled in a reply to an existing comment get brought back to the exact location on the page after they have pressed on a reply link. This link includes a html ancher which corresponds to the reply box id and works without htaccess. The url shows as following: http://localhost/test/index.php?lan=EN&page=blog&table_index=11#11 With htaccess it also works if I use the following rule: RewriteRule ^([^/]+)/blog/([^/]+)/([^/]+)/?$ index.php?lan=$1&page=blog&table_index=$2#$3 [NC,NE,L,R] But if i remove the R flag to get to a nice url it no longer works. I looked a lot but can't seem to find an answer. Does anybody have an idea?

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi bart van de ven,

That's quite a name you have there.

Whenever you use rewrite rules it's not to redirect from an ugly or cluttered URL but to get Apache to look for something that is already setup using the same expression structure as our rewrite rule, for example.

Below I have setup an anchor link using a rewrite rule.

<a href="/profile/chris">Go to Chris' Profile</a>

Now we need our RewriteRule

RewriteRule ^profile/([\w]+)$ view-profile.php?name=$1 [NC,L]

Essentially we don't want to be redirecting our clean URL back to an ugly URL as that defies the purpose of rewrite rules in the first place, in your case you simply just want to redefine your HTML links to point to the expression and no the original url.

RewriteRule ^/?blog/([A-Z]+)/([\d]+)/?$ index.php?lan=$1&page=blog&table_index=$2 [NC,NE,L]
<a href="/blog/EN/11/#11">View Blog Post</a>
<!-- or without the comment number -->
<a href="/blog/EN/11/">View Blog Post</a>

Hope that helps bud.

bart van de ven
PLUS
bart van de ven
Courses Plus Student 8,214 Points

Thanks a lot! The construct with the hash in the link works the other one for some reason keeps linking back to the top of the page. But for me the one with the hash is working perfect. Thanks once more as i spend days cracking it!

Chris Shaw
Chris Shaw
26,676 Points

Not a problem.

The construct with the hash in the link works the other one for some reason keeps linking back to the top of the page.

If no hash is given in the URL the page will automatically load at the top as you can't pass them through using rewrite, well because hash fragments are browser driven therefore the server would just ignore it and throw it away.