.htaccess mod_rewrite variables via redirection

Short version:

I wrote a question and realized that most people would not want to read this text. Consider the link below, here is TL; DR:

I need 301 redirect this URL http://app.com/search/foo-bar/

to this URL http://app.com/#!/search/foo-bar/

and send this: /foo-bar/ or something else /search/ to the server side of the script. In this case, it is written in php.

Edit for clarity:

Current answers seem to focus on rewriting hash bang. This part is not a problem. The problem is that I lose any associated data when rewriting to the hashbang url, since app.php will be displayed on the server side as a location, not app.php / #! / Foo-bar / - so I need to capture foo -bar, and send it to the server somewhere other than the URL. Rewriting works and it’s not a problem. Thanks for your answers though!


Long version:

Ok, so I have an interesting question that is hard for me to understand.

Scenario:

I have a backbone.js application that uses hashbang for state:

app.com/#!/search/search-term/key-value/foo-bar/

In addition, I have google traffic going to the site from the previous version, which will beat the URLs "pretty url":

app.com/search/search-term/key-value/foo-bar/

I use mod_rewrite.htaccess to replace the old url for hashbanged if the user clicks on an obsolete url.

I recently introduced a downloaded javascript version of a downloadable site on which the site will be built on top of which to gracefully lower and support the crawlers. This is written using php.

In order for the php site to work, I need to pass the hash group values ​​to the server side of the script, so I can figure out what to display.

Problem:

When I convert url and add anchor, everything that is behind the anchor (hashbang) is no longer sent to the request, so I do not have access to it in php.

RewriteRule search/?(.*) #!/search/$1 [R=301,NC,L]

My options for sending information to the server side are as follows: 1. Query string 2. Environment variables 3. Headers

So I tried sending things through the query string

RewriteRule search/?(.*) #!/search/$1?filter=$1 [R=301,NC,L]

Obviously, this did not work (the request is behind the anchor), so I tried it in front of the hash site

RewriteRule search/?(.*) ?filter=$1/#!/search/$1 [R=301,NC,L]

It works, but disgusting and redundant for the end user. So I decided to try environment variables.

RewriteRule search/?(.*) /!#/search/$1 [R=301,NC,L,E=FILTER:$1]

This did not succeed because environment variables are not saved through redirection (duh). I turned to using headers:

RewriteRule search/?(.*) /#!/search/$1 [R=301,NC,L,E=FILTER:$1]

Header set filterParams "%{FILTER}e"

But for some reason, the headers are not accepted by the page through redirection. This seemed to make sense (although I now went far beyond my comfort level with Apache directives), so I tried to repeat the header, hoping that it would be transmitted, received by a second rewrite (which the search could not find) and an echo.

RewriteRule search/?(.*) /#!/search/$1 [R=301,NC,L,E=FILTER:$1]

Header set filterParams "%{FILTER}e"

Header echo filterParams

Nada - the filter does not exist, therefore, although it makes it a server, it is zero. My next thought was to try to use some kind of conditional. Here is my attempt:

 RewriteRule search/?(.*) legacy.php/#!/search/$1 [R=301,NC,L,E=FILTER$1]` <FilesMatch "legacy.php"> Header set filterParams "%{FILTER}e" </FilesMatch> Header echo filterParams 

That didn't work either, so I'm at a standstill. I understand that I spent so long on it that I probably have a solution within my reach, and I’m just tired of looking at it, or it’s not even possible, even with a rough hack of the title.

Does anyone know how to do this?

+4
source share
2 answers

AFAIK, there is no good way to save variables through redirection without inserting them into the query string ...

0
source

rfc1738.txt says # is not a valid URL character

Additionally, apache docs says that # signals a comment in the apache configuration files.

short answer - your decision is broken not your implementation

0
source

All Articles