0

I am not sure just how to ask this...

www.example.com/ 

www.example.com/user/list

www.example.com/user/add

How can I make all paths get caught so I can have it go to example.com/index.php and then I can decide what files I want to pull in from the paths...

Instead of doing:

www.example.com/index.php?e=user&a=list

www.example.com/index.php?e=user&a=add

I want something like this:

www.example.com/user/list

www.example.com/user/add

I am not sure if this code will be on a windows server or Unix server. Not sure if I will have .htaccess or not.

Nathan Stanford
  • 1,336
  • 3
  • 23
  • 37

1 Answers1

1

you are looking for the .htaccess file !

the .htaccess is a simple file placed in the root directory and allows you to play with the directories and redirect any request to a specific file.

so for example, you can redirect any request for example.com/user/add to example.com/index.php?e=user&a=add

and the user will only type and see example.com/user/add in the address bar. but the actual file that is displayed is example.com/index.php?e=user&a=add

I will take you through the steps needed to achieve that:

  1. in order to display example.com/user/add instead of example.com/index.php?e=user&a=add first you will need to create a hidden file which starts with a period and then htaccess and that file should be placed in the main directory
  2. then open that file and write into it the following:
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^user/add index.php?e=user&a=add [NC]

Note:

but that will redirect the exact same url example.com/user/add. if you wanted the user to be a variable so it could be anything such as example.com/hello/add you will need to change your .htaccess content to this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)/add index.php?e=$1&a=add [NC,L]

so know {user} is a variable and will be redirected to index.php?e={user}&a=add

Faisal
  • 127
  • 1
  • 7
  • So one more quick question if I waned the add to be a variable as well. would i do this? RewriteRule ^(.*)/^(.*) index.php?e=$1&a=$2 [NC,L] – Nathan Stanford Jun 06 '21 at 22:02
  • if the path is www.example.com/app/users/add I am testing this for now on xampp can it be in a sub folder to cover children folders or does it have to be in the root only? – Nathan Stanford Jun 06 '21 at 22:17
  • @NathanStanford if you waned the {add} to be a variable as well, you would do this: RewriteRule ^(.*)/(.*) index.php?e=$1&a=$2 [NC,L] – Faisal Jun 08 '21 at 01:41