How to use .htaccess file to redirect # to page

Hello,

We want to redirect like this via .htaccess file:

www.anoox.com#sarah

to:

www.anoox.com/profile_view.php?nick=sarah

of course “sarah” is the example name which changes as per user account name.

How do we do this?

Thanks,

Capture the “#name” portion, then use that as the backreference.

How do you capture the “#name” portion?
Since $_GET[‘#’] will not work in above case.
So that is why I think only via .htaccess file commands can “#name” portion be captured.
Thanks,

And just to be clear, I would be find with also a URL like this:

www.anoox.com/sarah

with the question then being how do we capture the “name” part after / ?

Thanks,

WOW! No answer from anyone yet to this question! That is a shame :frowning:
So many could benefit from the answer to such as key question.

It’s only been a day. :wink:

If you’re having trouble with your search foo, this page should help you

https://wiki.apache.org/httpd/RewriteQueryString

If it’s a key question, have you searched the forums to see if it’s been asked and answered before?

(I can’t recall a similar thread, but my memory isn’t always great.)

TechnoB,

Yes, of course I have searched for this question online as well as here on Sitepoint. And no there is not such a question and answer.

Now provide some more info about this challenging question:

we have tried grabbing and parsing the URL using Php code:
parse_url($_SERVER['REQUEST_URI']);

to parse the URL for this purpose. But the problem with this is that Apache is sending straight to “Page Not Found” and not allowing the code in index.php parse the URL.

And we do not want to turn off Apache: RewriteEngine on
because in case of anything else but index.php we want them send to “Page Not Found” if the entered URL does not exist.

So how can we do this?

Thanks

Maybe this will be some help? (the NE no escape flag in particular)

http://httpd.apache.org/docs/2.2/rewrite/advanced.html#redirectanchors

Redirecting Anchors

Description:
By default, redirecting to an HTML anchor doesn’t work, because mod_rewrite escapes the # character, turning it into %23. This, in turn, breaks the redirection.

Solution:
Use the [NE] flag on the RewriteRule. NE stands for No Escape.

Discussion:
This technique will of course also work with other special characters that mod_rewrite, by default, URL-encodes.

1 Like

Is there anything here that helps?

Mitti,

Read that page and tried it, but cannot get it to work :frowning:
Amazing that it is 2018 and there is not a clear way to route www.xyz.com/jon_doe

to page fro jon_doe

well do so easily, since I have been looking to do this fro about a week now.
BTW, this is how pages re-directing works on for example Facebook. Which is basically what we want to do for our social networking members.

Thanks.

Please post
an example of what a browser HTTP request would look like.
an example of what the system file path would look like.
the section of the htaccess file you have now but isn’t working.

1 Like

I think the difficulty is you have previously been talking about www.xyz.com#jon_doe

1 Like

Not to mention that the # is being ignored if I can grasp that right. I’ve tried it before because I use the Facebook SDK and after logging in, you get #! and some other weird combinations that are generally ignored. I tried stripping those out and I couldn’t do it.

Gand,

I am not sure what you mean by your solution!
So again what we want to do is that when someone goes to URL:

https://www.anoox.com/john_doe

they are then redirected to the page for john_doe which is a long URL with many GET values in it.

How can we do this for GOD sake?
I have been trying to get the answer for this like 2 weeks now!

It can also be:

https://www.anoox.com#john_doe

Thanks

Agreed.

This code works in my MVC though it doesn’t quite produce a clean URL:

<a  href="/recommendNewItem/itemsByCategory/cereal#summary">

What does that mean!

uses php object oriented programming in a MVC framework with htaccess that allows clean URLs

do you use php objects?

Just wondering does any one have an answer to this key question?

That is again when given the URL of:

with john_doe being the name of the member and not a Dir or File, how can one grab the john_doe value before .htaccess “404 returns page not found”?

Thanks

I have just tested this on a site and it works OK:

.htaccess

RewriteEngine on
   RewriteCond %{REQUEST_FILENAME} !-f          
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/$1 [L] 

  ErrorDocument 404 /index.php

I think the .htaccess file ONLY traps URL parameters if they are not a specific file or directory and calls index.php .

Test:
index.php

<?php
 echo '<pre>'; 
   print_r($_SERVER); 
  echo '</pre>'; 
// 
// PHP script depending on paramaters 
//
exit;;