htaccess rule not working when adding slash - php

My main domain points to /html folder which has nothing in it but a .htaccess file.
I have a CodeIgniter website in /html/manage folder.
This is htaccess file content
RewriteEngine on
RewriteBase /
RewriteRule ^pg/(.*)$ /manage/product_groups [L,QSA]
This does not work, it shows 404 error page of codeigniter.
(If I visit website.com/manage/product_groups directly from browser, it works fine)
If I do RewriteRule ^pg/(.*)$ /manage [L,QSA] then it works fine, actually this page has login page of CI.
My main goal is to redirect every request redirect from website.com/pg/ to website.com/manage/product_groups

I didn't know if you had a special case or not.
Just referring to the routing doc examples.
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['pg'] = 'manage/product_groups';
or
$route['pg/(:any)'] = 'manage/product_groups';
Might have to experiment a bit.

Related

.htaccess rewrite to access php include in subfolder

I have this URL that give me two main parameters, folder and page, and som other tahat can variable to be present or not:
http://www.domain.com/adm/master.php?f=folder&p=page&otherparam1=somthing&otherparm2=somthing&otherparm3=somthing[...]
in my master.php-file I include a php-file based on these parameters:
$folder = $_GET['f'];
$page = $_GET['p'];
if(empty($page)) {
$page = 'dashboard';
}
$path = $folder.'/'.$page;
include_once 'pages/'.$path.'.php?otherparm1=somthing&otherparm2=somthing&otherparm3=somthing[...]';
The otherparam is to be multiple parameters given i the URL.
I want to rewrtite the URL to show somthing like this:
www.domain.com/adm/folder/page/somthing/somthing/somthing[...]
I have searched the web, but not been able to find a solution.
If you want to do it with simple .htacces then you can use following .htacces code:
RewriteEngine on
RewriteCond $1 !^(index\.php|css|favicon.ico|fonts|images|js|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Basically what this code do is overwrite your request to your domain (ex: localhost/mysite), instead accessing folder or file it will access your index.php. Except for index.php, favoicon.ico, robots.txt files and css,fonts,image and js folder or what you define in second line on my code above.
Try this in your Root/.htaccess
RewriteEngine On
RewriteRule ^adm/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /adm/master.php?f=$1&p=$2&otherparam1=$3&otherparm2=$4&otherparm3=$5 [NC,L]
$1 ,$2,$3... contains whateve value was matched in regex capture groups "([^/]+)".
This will rewrite
example.com/adm/folder/page/foo/bar/foobar
to
example.com/adm/master.php?f=folder&p=page&otherparam1=foo&otherparm2=bar&otherparm3=foobar
without changing the url in browser address bar.

.htaccess error redirecting to another directory

I'm having problem with .htaccess that rewrite my url. I created a simple dynamic website and I cant figure how to fix it. This is my login url localhost/foss/ then after successful login the user will be redirected to localhost/foss/main
here is the code of my .htaccess
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1
and here is my php that handle include files depending on the p value.
<?php
$page = $_GET['p'];
$pages = array('home', 'inv_view', 'inv_add', 'inv_delete', 'inv_edit', 's_view', 's_add', 's_delete', 'change_pass', 'register', 'logout');
if (!empty($page))
{
if(in_array($page, $pages))
include $page . '.php';
else
echo 'Page not found!';
}
else
include 'home.php';
?>
this is working fine, the only weird things that going on, is after the user is redirected to the localhost/foss/main, the .htaccess rewrite the url to localhost/foss/main/?p=main, but when i choose a link like localhost/foss/main/home it will work normally. how can i get rid of the ?p=main after the user login and redirected to localhost/foss/main
Reason why this behavior is happening because mod_dir module is running after mod_rewrite and adding a trailing slash in front of directory main/ after your rewrite rule and you get final URL as localhost/foss/main/?p=main.
To fix this issue have a trailing slash in your refresh tag:
header('refresh: 5; url = main/');

Subfolder routing not working

I am having trouble in routing my files. I need to access the new_account subfolder and the controllers inside it...
Here is the structure:
controllers
---new_account
---------step1.php
---------step2.php
---accounts.php
---pages.php
This is the content of my .htaccess file located outside application
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
And here is my routes.php
$route['default_controller'] = 'pages/show_page/home';
$route['pages/(:any)'] = 'pages/show_page/$1';
$route['accounts'] = 'accounts';
$route['new_account/(:any)'] = "new_account";
$route['(:any)'] = 'pages/show_page/$1';
$route['404_override'] = '';
I also use $config['uri_protocol'] = 'REQUEST_URI';
My pages controller is working perfectly, even as direct links e.g. <?php echo base_url(); ?>home redirects to ../pages/home.php.
The accounts.php controller calls new_account/user_home.php that contains guidelines and a link to the actual form. The link is <?php base_url();>new_account/step1 and it does not work which should have because of $route['new_account/(:any)'] = "new_account";.
Whenever I click the link to new_account/step1, I get redirected to the main home page.
I really want to access app_name/new_account/step1 and the others in the directory...Did I miss something in these files or an integral step?
NOTE:
I also tried this subfolder routing extension by inserting MY_Router.php in the application/libraries folder but no changes happened. I then moved it into application/core but all I get is a server error.
Comment this line in your routes.php:
$route['new_account/(:any)'] = "new_account";
Codeigniter default routes should work for http://YOUR_SITE/new_account/step1 cause you have a controller file called step1.php inside the folder new_account. Also check that the name of the class of your controller inside step1.php is Step1

several conditions in apache mod rewrite to make custom URLs

In my domain example.com , I want to put all page files in a directory at root for example named file. So for example I put about.php the file directory and want to let users to access this page with this URL : example.com/about
or for example put user directory in the file directory and put login.php in it , and want to let users to access it with : example.com/user/login
In fact I want to remove .php and the file from the URLs.
AND
If those files don't exist , it should load a default file like index.php at root. for example the URL example.com/blabla should be mapped to the index.php
In fact , I want to make two conditions in mod rewrite with the mentioned priority.
notice : of course I should be able to use variables like $_GET at files like about.php
UPDATE : in summary , it should work with this logic :
if the URI isn't a file or directory{
if file/URI.php is a file
load file/URI.php
else
load index.php
}
some body gave an answer to use ErrorDocument 404 index.php , but it's a really bad idea (and now has deleted his answer !)
Thanks for your help...
A conditional rewrite (if the pages exist) sounds like a bad and complicated idea, if it even is possible.
I would rewrite everything to /index.php and control everything from there:
If the components of the url lead to an existing page, use / include that content;
If no valid page is found, present your original index content.
Example redirect rules for your situation:
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^.*$ /index.php?id=$1 [QSA,L]
Will include the requested path in id and append the variables from the original query string (the QSA flag).
More specific for your /user/login example would be:
RewriteRule ^([\w-]+)/([\w-]+)$ /index.php?path=$1&file=$2 [QSA,L]
[\w-+] : any word character (including _) and the -

How to access a file via htaccess?

I have a folder structure like
Controller
|_check.php
View
|_ .htaccess
|_ index.php
|_ Webroot
|_ js
|_common.js
.htaccess
In .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ View/ [L]
RewriteRule (.*) View/$1 [L]
</IfModule>
In View/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
The outer .htaccess file links to View folder and there View/.htaccess links to index.php.
There is button in index.php(view file) with ajax to check.php.
I have given common.js The corresponding ajax function url as
var url = 'check.php'; // OR '../Controller/check.php'
The problem is the ajax is not working properly with two urls.
Whether I need change the folder structure to correct it or do I need to alter any htaccess file for accessing the check.php?
It's not about the folder structure but rather the htaccess rewrite rules that are wrong.
Your htaccess in the "root" redirects all requests to the View folder (which defaults to index.php, I assume)
I don't understand what you're trying to accomplish, if you explain I might be able to help you.
In your current setup, you can't access any file besides View/index.php (even when passing GET argument url)
EDIT:
In that case, if you wish to View/index.php be the only file accessible and force people to pass through View/index.php file, you can use PHP session variable.
Something like this...
in the top of your view.php file:
session_start();
$_SESSION['viewCheck'] = true;
//rest of view.php code
in your check.php code (or the file you're trying to access via AJAX)
session_start();
if (isset($_SESSION['viewCheck']) && $_SESSION['viewCheck'] === true) {
//Code of check.php
} else {
//Error message or redirect to view.php, for instance
//error message example
header('HTTP/1.0 401 Unauthorized');
//Redirect example
header("Location: http://www.yourhost.com/View/index.php");
}
NOTE 1:
You should remove the rewrite rules of your htaccess files.
NOTE 2:
Keep in mind that this is not bullet proof (and can be spoofed) since:
If someone visits View/index.php then he can access check.php freely. This can be mitigated if the session is killed after the ajax request. You can accomplish that if the ajax request consists in 2 requests, for instance, one to get a session key which expires in 10 seconds for instance, and then use that key to obtain the results from check.php
Session can be spoofed too (read more here)
Why dont you just set the path for the domain on the "View" folder, so noone can access you controllers etc. ?
I think this should also be more simple than playing around with .htaccess !
This is the way most PHP Frameworks do it..
And if you need to access the functions of check.php you can make a "ajax.php" that checks if the request is ok, and then uses "check.php" to catch the result!

Categories