$.post to Codeigniter Controller/Method 404 Page Not Found - javascript

I have developed a Codeigniter prototype application and hosted with Godaddy.com during the development phase for testing purposes without issue. This application uses a combination of javascript, jquery, HTML5, CSS and PHP (Codeigniter Framework). This week I have transferred the application to the clients server and have had many configuration issues but finally hit a wall.
The client set up a fresh install on a new Linux/Apache2 server with PHP5. mod_rewrite is enabled and allowoverride is set to all. The Apache2 settings (per the client are)...
My .htaccess script(since this issue I have tried many different .htaccess versions)...
RewriteEngine On
RewriteBase /CI/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
My javascript code for posting serialized form data to PHP page...
$.post('login_ctrl/authenticateuserlogon', $formData, function($responseData)
{
$userAuthentication=$responseData.userAuthentication;
if ($userAuthentication=='success')
{
window.location.href = "student_portal_ctrl";
}
else
{
$('#loginErrDiv').text('Username or Password is incorrect - Please try again.');
return;
}
});
My codeigniter file structure is...
-root /
/CI
/application
/system
/index.php
/license.txt
/.htaccess
When I post to this controller/method, the browser outputs...
-----EDIT-----
Sorry, forgot to include my config.php settings, here they are...
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; //I have tested the app using all of codeigniters
//default URI Protocols
-----EDIT #2-----
This is the method on the controller that is receiving the 404 error
//AUTHENTICATE USER LOGIN FUNCTION
public function authenticateuserlogon()
{
//SETTING $FORMDATA TO SERIALIZED POST DATA
$formData = $this->input->post();
//Sets the Method for the login_mdl
$modelMethod=$formData['modelMethod'];
//LOADING USER AUTHENTICATION LOGIN MODEL
$this->load->model('login_mdl');
//SENDING FORMDATA TO MODEL AND RETURING DATA TO $RESPONSEDATA
$responseData = array();
$responseData = $this->login_mdl->$modelMethod($formData);
//OUTPUTTING JSON RESPONSE DATA BACK TO JAVASCRIPT
$this->output->set_output(json_encode($responseData));
}

Use this .htaccess which is tested by me and working fine
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /CI/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [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>
and in your config.php file use this,
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];

Related

Laravel 8 and running on the server

I have a problem with running Laravel 8 on the production server.
In the root directory I put the .httpacces file from the public directory and changed the name of the server.php file to index.php
Everything works except styles, js, and more
The contents of the .httpacces file
Options -MultiViews -Indexes
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
What do I have to change, add? Thank you very much for the hint.
You do not want to rename the server.php. Whether you are using Apache or nginx, make sure your Documents Root points to the public!! folder of your Laravel application. Never point it to the main directory. You can find configurations for nginx and apache online regarding laravel.

Website keeps requesting a 301 URL address

My website is giving me a "This website is not secure" and I can't find the root of it, even though I double checked through everything to see if there are any http and switched them to https. I also have cloudflare forcing https.
I have checked the network tab and found one URL giving me trouble but it doesn't give me much information
The URL is giving a 301 moved permanently. I don't know where in my html I am requesting this site. I even used ctrl + f nothing showed up.
It's just requesting http://mywebsite.com/ but there is nothing I can see in my html requesting a http version of my site. I don't have anything that would cause my website to request http in .htaccess either.
RewriteEngine On
Options -Indexes
RewriteRule ^folder/?$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]
ErrorDocument 403 /index.html
ErrorDocument 404 /index.html
ErrorDocument 500 /index.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/403/$
RewriteRule ^(.*)$ /index.html [L]
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /index.html [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /index.html [L]
Is there something I'm missing?
I found the issue. It looks like it was jQuery that was requesting a http version of my site.
I couldn't ctrl + f it because it was a cloudflare jQuery link.
https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js
On line 2727 it was checking using http. (The picture shows https is because I just changed it)
The Fix
Save the jQuery locally and change http to https.

.htaccess file won't work on my node.js app

I have the following .htaccess file located in the root directory of my node.js app:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
My website runs at www.mywebsite.com. When a form is submitted, the page is redirected to a success page:
res.redirect("/success.html");
The problem is, my .htaccess file isn't working properly. I am redirected to www.mywebsite.com/success.html, instead of www.mywebsite.com/success.
I have tried changing my node.js script to:
res.redirect("/success");
But then I get an error page. What am I doing wrong?
.htaccess is only for Apache servers, but you can try something like this:
nodejs equivalent of this .htaccess

$_GET request is returning undefined

I have an angular project that is trying to connect to google calendars, this way I can submit bookings and they get automatically added the the gmail calendar. This is working but not in the correct scenario. This is the example I have followed to get it working.
I have a angular project that on login will check if the web application has permissions to use the calendar. On return it uses a GET to save the token to the session, however my $_GET is undefined when returning with the redirectUrl. Even when the param is the in URL. I believe this to be down to angular routing.
At login i check if the $_SESSION['token'] has been set, if not redirect through javascript. $authUrl = $this->client->createAuthUrl(); and then a location.href = $authUrl to redirect in javascript. this works fine and user accepts permission and redirects back to the next page which is www.example.co.uk/index/dashboard?code=4%2F5NgsMp8JepEmXVgR8pez0fQdk_jTPonsuExdV8&authuser=0&session_state=2dfb836cfc217135f907c7e6c0d9c526983b6f..9103&prompt=none
But when I try a $_GET['code'] this is undefined which means this method cant run which sets the token:
if (isset($_GET['code'])) {
$this->client->authenticate($_GET['code']);
error_log("tou can access");
$_SESSION['token'] = $this->client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
print_r($_GET); fields:
Array ()
.htacces rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php/#/$1
</IfModule>
UPDATE
This the error was because i was loading a separate php from an ajax request which was not being set as the header. once i relocated the GET request to be inserted into the file that matched the header, it worked.
You need to include the query string flag in your rewrite...
https://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php/$1 [QSA]
</IfModule>

remove the index.php WAMP

I have a site and it works to type "www.mysite.com/folder/" (if i have a index file in that folder).
It also works to type "www.mysite.com/folder/index.php"
All fine and good, but I want it so if you type /index.php it will remove the index.php on the end also if I use a form and post to index.php I don't want that to show in the url.
I have looked around and seen every solution say .htaccess rewrite, but It don't seem to be working / or its just not the same thing that I want?
or do I need to use javascript and update the address bar?
(I'm using WAMP)
EDIT:
Things I've have tried:
1
httpd.conf
AllowOverride All
2
I have enabled:
Apache modules> rewrite_module
3
I have created a .htaccess placed in my wamp/www folder
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
4
wamp/apps/phpmyadmin/config.inc.php
added
$cfg['index_page'] = '';
EDIT 2:
This look like it works, but it messes up my POST to index.php so it breaks my login (It don't start the session).
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
An .htaccess file is definitely the correct solution. If it isn't working, try one of these possible answer:
.htaccess not working on localhost with XAMPP
How to use .htaccess in WAMP Server?
.htaccess not working on WAMP
Update: Re-reading your question, you might be using an older htaccess script (for PHP < 5.2.6 version). I'm not sure if the script will work in current versions. Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
The ? after index.php in final line is the difference
If above doesn't help, what version of PHP does the PHP command phpinfo() report?
<?php
phpinfo();
die();

Categories