684425 Posted July 14, 2020 Share Posted July 14, 2020 I have URLs of my project on my localserver like localhost/shareboard/users/login in which (users is my sub directory and login is php file named login.php) I am already using the following htaccess rules for this Options +FollowSymLinks RewriteEngine on RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L] Is there a way of removing sub directory only from URLs without changing above htaccess? or some other idea that gives result as localhost/shareboard/login (In my php code i am using the above address in hyperlinks as <a href="<?php echo ROOT_URL; ?>users/login">Login</a> And the following lines are added in my config.php file define("ROOT_PATH", "/shareboard/"); // I am using this one for linking files define("ROOT_URL", "http://localhost/shareboard/"); // and using this one in hyperlinks Please guide me if there is a possible solution for what I want to do. Thanks🙂 Quote Link to comment https://forums.phpfreaks.com/topic/311081-php-htaccess-url-rewrite/ Share on other sites More sharing options...
requinix Posted July 14, 2020 Share Posted July 14, 2020 You cannot change the RewriteRule and also not change it 😉 If you want to support /shareboard/login -> controller=shareboard, action=users, id=login, then add a new RewriteRule to do that. That's all it takes. Because it's okay to have more than one of these as long as they match different URL patterns. When you do that, please clean them up so less stuff is optional. Because you have a problem with being too flexible on what you're matching. Try going to some invalid URLs and see what happens. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311081-php-htaccess-url-rewrite/#findComment-1579634 Share on other sites More sharing options...
684425 Posted July 15, 2020 Author Share Posted July 15, 2020 Thank you for your reply. For some invalid URLs i have created a class in php class Bootstrap{ private $controller; private $action; private $request; public function __construct($request){ $this->request = $request; if($this->request['controller'] == ""){ $this->controller = 'home'; } else { $this->controller = $this->request['controller']; } if($this->request['action'] == ""){ $this->action = 'index'; } else { $this->action = $this->request['action']; } } public function createController(){ // Check Class if(class_exists($this->controller)){ $parents = class_parents($this->controller); // Check Extend if(in_array("Controller", $parents)){ if(method_exists($this->controller, $this->action)){ return new $this->controller($this->action, $this->request); } else { // Method Does Not Exist echo '<h1>Method does not exist</h1>'; // This line can be changed, redirect to a method that exists return; } } else { // Base Controller Does Not Exist echo '<h1>Base controller not found</h1>'; // This line can be changed, redirect to a base controller that exists return; } } else { // Controller Class Does Not Exist echo '<h1>Controller class does not exist</h1>'; // This line can be changed, redirect to a controller class that exists return; } } } For the rest of invalid URLs an error message "Not found is thrown". I am thinking of handling this via htaccess because i am failed to find a solution for handling this via php. Quote Link to comment https://forums.phpfreaks.com/topic/311081-php-htaccess-url-rewrite/#findComment-1579639 Share on other sites More sharing options...
requinix Posted July 15, 2020 Share Posted July 15, 2020 10 hours ago, 684425 said: I am thinking of handling this via htaccess because i am failed to find a solution for handling this via php. ...Yes. You know how you have your RewriteRule that sends a particular URL pattern to index.php? You do not have one for the new URL pattern. That means it won't go to index.php. And PHP cannot do anything if it doesn't get the request. You don't need any PHP for this. All you need is a new RewriteRule for the new URL, and it can still use your index.php with controller and action and id by simply specifying a default value for the one that's missing. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311081-php-htaccess-url-rewrite/#findComment-1579653 Share on other sites More sharing options...
684425 Posted July 16, 2020 Author Share Posted July 16, 2020 Thank you sir, The issue is resolved. Your advice helped me. i am checking URLs now.🙂 Quote Link to comment https://forums.phpfreaks.com/topic/311081-php-htaccess-url-rewrite/#findComment-1579675 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.