htaccess rewrite and submit form not working together - php

after several researches a give up and post here , if someone could clearify me a things , it would be nice, thanks
When i use http://site.com/file.php?url=2012/08/15/My-html-page all works fine , ajax sends a post to file.php and display it , but when i use a rewrited by htaccess url http://site.com/2012/08/15/My-html-page.html post form do nothing , it dosent work
Links:
Original : http://site.com/file.php?url=2012/08/15/My-html-page
Rewrited with htaccess: http://site.com/2012/08/15/My-html-page.html
The database entry : 2012/08/14/My-first-post
To add a html agter pages i added
$url=$url.'.html';
$url= $_GET["url"];
My ajax that in file.php
<SCRIPT language="JavaScript">
$(function() {
$(".comment_button").click(function()
{
var element = $(this);
var note = $("#note").val();
var dataString = 'note='+note;
if(note=='')
{
$('#flash').fadeIn("fast");
}
else
{
$.ajax({
type: "POST",
url: "file_post.php",
data: dataString,
cache: false,
success: function(html){
$('body').load('/<? echo $url ?>.html');
}
});
}
return false;
});
});
</script>
my form that in file.php
<form name="comment" method="post" action>
<button class="comment_button" type="submit"><span>Отправить</span></button>
<textarea name="note" id="note"></textarea>
</form>
My php file in file_post.php
if(isSet($_POST['note'])) {
$note=$_POST['note'];
$note = html_entity_decode($note);
$date=date("Y-m-d");
$time=date("H:i:s");
$table = "insert into comment (note, date, time) values ('$note', '$date', '$time');";
mysql_query($table);
}
My htacces
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteBase /
<Files inc/config.php>
deny from all
</Files>
RewriteRule ^([a-zA-Z0-9-/]+).html$ file.php?url=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ file.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
the $_SERVER['REQUEST_URI'] on rewrited page gives me a empty input , its normal ?

Try changing your 2 rules to:
RewriteRule ^([a-zA-Z0-9-/]+).html$ file.php?url=$1.html [QSA,L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ file.php?url=$1.html [QSA,L]
The .html is getting stripped so your file.php script is getting called like:
http://site.com/file.php?url=2012/08/15/My-html-page
instead of
http://site.com/file.php?url=2012/08/15/My-html-page.html

i dont know what was the error , something with a slashes or numbers and htaccess
but i found a solution with
RewriteRule ^read/([A-Za-z0-9\-_]+]*)$ file.php?url=$1 [L]
without html or php

Related

Rewrite URLs in RESTful API

I have to create an API in PHP, which I have never done before. I have a index.php in my root folder which contains a form. A button click activates an AJAX Request to read all entries and a form can be filled out to send data. I will also have DELETE and update.
ajaxcall.js
// GET to retrieve
var req;
req=new XMLHttpRequest();
req.open("GET", 'src/api/v1/posts',true);
req.send();
//post with ajax
$.ajax({
type:"POST",
url: "src/api/v1/posts",
data: test,
ContentType:"application/json",
success:function(){
alert('successfully posted');
},
error:function(){
alert('Could not be posted');
}
});
I have a folder src/api/v1 in there I want the endpoint file api.php which handles the different requests. I now need all the AJAX calls to be resend to api.php which sits inside that v1 folder. I created an .htaccess which is stored in also stored in src/api/v1. I added the following:
.htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule src/api/v1/(.*)$ src/api/v1/api.php?request=$1 [QSA,NC,L]
Unfortunately I get a 404 not found, it just does not seem to re-write the URL to the api.php so it is looking for /posts which does not exists. What am I doing wrong?
Actually you should store the .htaccess on the root directory, otherwise Apache will not know that there's an .htaccess under /src/api/v1, so apache will look for the directory src/api/v1/posts and as is it's not found a 404 error will be returned.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule src/api/v1/(.*)$ /src/api/v1/api.php?request=$1 [QSA,NC,L]
</IfModule>

Codeigniter cannot refresh page after session destroy and direct

I have a PHP program written on code igniter that needs your help! Have been trying for 3 weeks!
I have the htaccess mod rewrite to make http://www.sampleurl.com/controllername instead of http://www.sampleurl.com/index.php/controllername
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</IfModule>
I have a controller for Dashboard (currently used for testing the session.)
public function index()
{
$is_logged_in = $this->session->userdata('fb_session');
$data = array(
'fb_data' => $is_logged_in,
);
if (!isset($is_logged_in) || $is_logged_in != true){
redirect('/error');
}
}
Below is the function that suppose to kill the current session and redirect to dashboard page.
$('#logoutbtn').click(function(){
$.ajax({
type:"POST",
url: "/fbcontroller/killsession",
datatype: "json",
success: function(){
alert("Logout");
}
});
});
public function killsession(){
$this->session->sess_destroy();
redirect('/dashboard');
}
Problem 1: as I redirect from function in 1 controller to another, the redirection fails here. Instead of directing to dashboard, the firebug displays 404 error, page not found. And in the response, it displays all the HTML code of /error page. Does it mean that the redirection works? If yes, why wouldn't it display on browser?
Problem 2: session destroyed, but logged in page stays even as I refresh (F5).
For htaccess I suggest:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
And know that when you kill session CI will create a new 1 on your next Page load (in your case on the redirect) .. Session isn't just for user logins.
Don't forget at app/config/config.php to set
$config["index_page"] = '';

how to handle links path in a dynamic site using mod_rewrite for clean url

say i have a function:
function ajax()
{ $.ajax({
type:'POST',
url:'index.php',
data:{t:t},
success:function(data)
{ if(data)
{
alert(data);
}
}
i am using this function all over my pages,the problem is when i am in homepage
i.e :www/mysite.com it is fine but when i am in a page like this
www/mysite.com/modrewrite where modrewrite is not a folder but instead a mod_rewrite thing the ajax function returns 404 error,but when i modify it to ../index.php then it does not work in homepage.what should be done to make it work in all the pages!
Try this and see how it works for your ajax or use the full path.
function ajax(){
$.ajax({
type:'POST',
url:'/index.php',
data:{t:t},
success:function(data){
if(data){
alert(data);
}
}
});
}
Then in your .htaccess use this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-z0-9.]+)$ page.php?uri=$1

User Friendly Url Ajax post

Problem solved
I'm using friendly url and code which is described below. I want to Request php file with ajax post. Does anybody know how to do that?
For example: I have code below:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "ajax/test.php",
data: "data=test",
success:function(data){
alert(data);
},
});
});
When program post data to test.php file then i want to also return data from this file.
Url: http://mysite.com/admin/admin.php?m=users&a=edit&id=12
User Friendly Url: http://mysite.com/admin/users/edit/12
Problem solved
I am confused what you are asking about. I will assume that you just want to return some data from test.php.
if so then assuming your $data variable holds all the data, you can do this
echo json_encode($data);
and that should alret you back the data as you have it in your code
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /directory_path/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
</ifModule>
That's .htaccess file for user friendly url.
Thanks for help!

Ajax not working with this .htaccess

Im developing a CMS with PHP and MVC. I have the follow htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
So i have a dispatcher to go to diferents views depends of the permalink on the url.
My problem is that im trying to use ajax for a Login and few things more and cant make it works.
Is there any expception in .htaccess to make this work or i only can put more exceptions on dispatcher to load the .php in ajax?
Anyone can help me?
Thanks in advance.
EDIT:
$.post
(
"../ajax/login.php",
{
u:Base64.encode(user),
p:Base64.encode(password),
},
function(data)
{
},
"json"
);
I tried with:
"../ajax/login.php"
PATH+VIEW+THEME+"/ajax/login.php"
My folder structure is:
view
themes
standar
ajax
login.php
js
ajaxInteractions.js
index.phtml
As stated in the comments, you need to check your path or just use your absolute path like this:
$.post
(
"/view/themes/standar/ajax/login.php",
{
u:Base64.encode(user),
p:Base64.encode(password),
},
function(data)
{
},
"json"
);
And one more thing; it is not always best practice to spread out the code like this ( it's good for PHP and plain JS calls ). For these types of calls, try to keep it more concentrated:
$.post( "/view/themes/standar/ajax/login.php", {
u:Base64.encode(user),
p:Base64.encode(password),
}, function(data) {
/** do your stuff **/
}, "json" );

Categories