0

I have theses URLs

http://www.website.com/?goto=plaforms
http://www.website.com/?goto=offers&platform=dates
http://www.website.com/?goto=profile&member=1
http://www.website.com/?goto=product&offer=2

I want to change them using .htaccess to be like this:

http://www.website.com/plaforms
http://www.website.com/offers/dates
http://www.website.com/profile/1
http://www.website.com/product/2

I've tried this but not working

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?goto=$1

Honestly I have no clue how to do it.

UPDATE: I have an index.php in which I call all pages

index.php

$page = $_GET['goto'];
$url  = $page.'.php';

if(file_exists($url)):  
  include($url);
else:
  header('location:'?goto=home');
endif;

The 1st param ?goto= calls for pages and the second param call data from database

Anas Amine
  • 115
  • 1
  • 2
  • 6
  • I believe your answer is [here](https://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php) – Julius Nov 26 '20 at 18:40
  • Good that you have shown your efforts in your question. Could you please do let us know what is the logic of having different different query string values like `&member` and `&offer` for better understanding of the question. – RavinderSingh13 Nov 26 '20 at 18:41
  • 2
    The problem description "not working" isn't working. – mario Nov 26 '20 at 18:47
  • @RavinderSingh13 I updated my question. hopefully will help for better understanding – Anas Amine Nov 26 '20 at 18:47

1 Answers1

2

You could write rewrite rules like these:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#The rules
RewriteRule ^([^\/]+)?$ /?goto=$1 [R=301,L]
RewriteRule ^offers\/dates$ /?goto=offers&platform=dates [R=301,L]
RewriteRule ^profile\/(\d+)$ /?goto=profile&member=$1 [R=301,L]
RewriteRule ^product\/(\d+)$ /?goto=product&offer=$1 [R=301,L]

The problem is some of the parameter values can not be derived from your fancy URLs. Therefore, you have to spell them out fully (like /offers/dates) or partially like /profile/x and /product/x). The only really generic rule is the first one that rewrites anything /xxx to goto=xxx. So, depending on y our URL space you may have to write a lot of rules.

wp78de
  • 18,207
  • 7
  • 43
  • 71