Reputation: 61
I'm trying to rewrite all the URL on my repertory with HTACCESS. I want them to have the same model/architecture. I found some code on different websites, but there are not working when I try them.
Here is what I want to do :
I have this URL : localhost/projects.php?id=1 I want it to look like this : localhost/projects/1
Here is another exemple :
I have this URL : localhost/profile.php?id=1 I want it to look like this : localhost/profile/1
In other words I want to transform this :
to
Is there any way I can do this with all my pages ? I don't know what I should write on my HTACCESS file.
Upvotes: 0
Views: 50
Reputation: 2138
First of parsing all route with .htaccess will be a bloody pain, you should just handle requested file and its parameters then you should parse parameters inside your php code (this is called as routing) and you will create your routing for your application.
Please check the .htaccess rule below;
#These are our conditions to run our rewrite rule
RewriteCond %{REQUEST_FILENAME} !-f #Request is not a file
RewriteCond %{REQUEST_FILENAME} !-d #Request is not a directory
RewriteCond %{REQUEST_FILENAME} !-l #Request is not a link
#This is our rewrite rule
RewriteRule ^([a-zA-Z0-9]*)?/(.*)$ $1.php/$2 [QSA,L] #QSA means QueryStringAppend modify according to your need, L means this is my last rule which same with stop processing rewrite rules
And you should have a php code something like below which process the request for your routing
<?PHP
$requestedPath = trim($_SERVER["PHP_SELF"],"/");
$request = explode("/",$requestedPath,2);
$requestFileName = $request[0]; //This is your requested file as you know already from .htaccess
$requestParams = $request[1]; //You should parse this params according to your routing need
?>
Hope these informations helps you about both routing and generic .htaccess rule.
PS: Your request will be like www.example.com/myaction/first-parameter/second-parameter/third-parameter and for this example $requestParams will be first-parameter/second-parameter/third-parameter
Upvotes: 1