Jump to content

php htaccess url rewrite


684425

Recommended Posts

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🙂

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.