.htaccess not redirecting to ?_escaped_fragment_= - ajax

I want to redirect the request "index.php?_escaped_fragment_=about" to "snapshots/snap_about.html"
but I can't get it to work :(
It's an AJAX site; the URL I receive when I try to request example.com/index.php?_escaped_fragment_=about is:
example.com/index.php?_escaped_fragment_=about#!about
This is my htaccess
redirect index.php?_escaped_fragment_=about http://www.example.com/snapshots/snap_about.html
redirect /testx.html http://example.com/dummytest/index.html
The redirect /testx.html works.
Maybe I am making a big big mistake, but I am new at this!

I'd probably do this using a RewriteRule. Is this a one-off, or is it a general pattern that could be applied to other URLs? e.g. would you also want
index.php?_escaped_fragment_=blah to redirect to snapshots/snap_blah.html?
If not, something like this:
RewriteRule index\.php\?_escaped_fragment_=about snapshots/snap_about.html [NC,R=301]
If yes, something like:
RewriteRule index\.php\?_escaped_fragment_=(.*) snapshots/snap_$1.html [NC,R=301]

Related

Rewriting url with the following regular expression

I'm trying to do a mod rewrite to get this url: localhost/test/index.php?hello
I created a file for this page called hello.php and it is in the folder /test
To clarify, I have another page that has a link to my hello.php, but what is the correct url so I can display localhost/test/index.php?hello in the url when I click the link to access my hello.php page.
The following doesn't seem like it is right:
RewriteRule ^.*$ index.php?$1 [L]
Try this if you want to just do php files.
RewriteEngine on
RewriteRule (.*)\.php$ /index.php?$1 [L]
To clarify what my answer does. It gives you more friendly URLs which it sounded like what your were asking for.
So you can use localhost/hello.php and it will be internally redirect to /localhost/index.php?hello. Internally means they will never see localhost/index.php?hello and will always see localhost/hello.php in their browser.
You can do any URL and it will rewrite to a php file. e.g. localhost/index.php?newpage and you can use /localhost/newpage.php
Hope that is clearer.
EDIT
You want the reverse but I don't know how your PHP is constructed but query strings are typically field/value pairs. For example name=john, page=contact, action=hello etc. You have index.php?hello but hello has no content or value.
That's probably why you're having such a hard time even re-writing your URL. Using $_GETwould require a value.
So what I would do, is if your URL was like this using field/value pairs
index.php?action=hello
Then in the index.php file you could do something like
$action = $_GET["action"];
if($action == "hello"){
//show contents of hello, include a page or whatever
}
Once you have good URLs it would be easy to rewrite it.
So if the URL that you want shown is like
index.php?action=hello and you want to redirect it to hello.php
Your .htaccess would look like this
RewriteRule ^action=([^/]+) /$1.php [R,L]
That would redirect it to the php file. If you don't want to show the redirection and keep it an internal redirect you can remove the R flag and just keep [L].
I personally don't want the user to see really long query strings URL example. mysite.com?page=music&artist=someartist&title=sometitle
So all my URL's are rewritten to be shorter and friendlier like my original answer.
you don't need .htaccess for 2. as far as you're using GET parametr - use it in index.php:
if (isset($_GET['hello'])) include('hello.php');
this will show the contents of hello.php inside index.php

mod_rewrite handling extra GET variables

I have a mod rewrite rule for pagination of my content:
RewriteRule ^content/page/([^/]*)$ /index.php?page=$1 [L]
However, I am also attempting to handle facebook connect, which will result in a url that looks like this:
http://content.local/page/2?session={"session_key"...
However, with my current rewrite rule, the session variable does not get passed along. How can I pass that variable in addition? I would want to un-rewritten link to look something like /index.php?page=2&session={"session_key"...
Use the QSA flag like this:
RewriteRule ^content/page/([^/]*)$ /index.php?page=$1 [QSA,L]

mod_rewrite rule for anchors

I want to rewrite a url to a url with anchors
from "topic/14/599" to "topic.php?id=14#599"
this is my current rewriterule:
RewriteRule ^topic/([0-9]+)/([0-9]+)$ /topic.php?id=$1#$2 [NE,L,R]
this works fine, but the rewritten rule is in the adressbar !
so the browser's addressbar changes from "topic/14/599" to "topic.php?id=14#599"
I would prefer to keep the "simplified" version in the addressbar, the rewriterule however doesn't work without the L,R-flags, I just can't get it to work.
(if I just rewrite topic/14 to topic.php?id=14 without any flags it works just fine)
Update:
RewriteRule ^topic/([0-9]+)/([0-9]+)$ topic.php?id=$1&a=$2 [NE]
now works! the addressbar looks to the user like: mydomain.com/topic/14/599
and internally it is redirected to topic.php?id=14&a=599 and once the page is loaded, javascript jumps to the element with id=599!
Since anchors are processed on the client side, there's no way to rewrite to an anchor "silently" like you're trying to do -- the anchor has to be in the user-visible URL at some point. You could rewrite topic/14/244 to topic/14#244 as a redirect and rewrite topic/14 internally to topic.php?id=14, but you can't do both in a single step.

Getting the original REQUEST_URI when using mod_rewrite AND mod_proxy

I'm using a custom.conf file for rewrites and codeigniter for some features of the site, mainly the articles.
My original url gets rewritten, so I have http://example.com/article-a101, this uses the custom.conf file to rewrite to codeigniter/article/read/101. I think I must send this as a proxy call using the [P] flag in mod_rewrite to make it rewrite again in codeigniters .htaccess file. Once it hits the code igniter .htaccess, it uses that mod rewrite structure to call the index file and use the article controller and the read function sending in the 101 as the parameter.
What I'm trying to figure it is how do I get the original url in the address bar as its not in the $_SERVER variable. Since I use the [P] on the first rewrite, request_uri has codeigniter/article/read/101.
custom.conf
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2 [P,L]
codeigniters .htaccess, fairly basic
RewriteRule ^(.*)$ index.php?/$1 [L]
Here's my current solution that I know there must be a better method for
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2?orig_url=%{REQUEST_URI}&%{QUERY_STRING} [P,L]
This stays hidden from the user, and I can access the original url through the query string, but doesn't seem like an elegant solution.
I'm pretty sure you cant do it any other way with mod_rewrite
but you could do it with codeigniter routing.
$route['^([_a-zA-Z0-9-]+)-a([0-9]+)$'] = "article/read/$2";
assuming your controller is named article and your function is named read
if you visited /article-a101
then $this->uri->uri_string(); would return article-a101 (the original url, which should be in your url bar now)
and $this->uri->ruri_string(); would return article/read/101 (where you actually are)

How do I use POST with htaccess rewriterule?

I have a rewriterule in my htaccess file that looks like this:
RewriteRule ^Signup$ /dst2/signup.php
I would like to use POST instead of GET for the data that gets passed by this form.
The rule above, however, sends the data as a GET.
Is there a way to use a POST with a rewriterule?
There is no way to reroute a GET action to a POST action with mod_rewrite. If you're sending a POST through a rewrite rule, it should end up being a POST when you're done. If you're sending a GET and trying to change it into a POST, change your system.

Resources