-2

actually I'm trying to build up a website to get better in coding but no matter how much i am reading, I dont get how to rewrite paths.

Any tipps how to rewrite http://domain/login/php/login.php to http://domain/login.php ?

Best regards, a Saltyy noob :c

Saltyy
  • 1
  • 5
  • 2
    There are many ways by which you can rewrite your path in PHP. But first comfirm that are you using any Framework or writing in Core PHP ? – Mohd Belal Dec 29 '17 at 18:50
  • i dont use any frameworks. Acutally i tried to rewrite it with ".htaccess" but I don't rly get how to use it ^^" ps. I hope my english isn't the worst :/ – Saltyy Dec 29 '17 at 18:52
  • Ok then i am explaining to you about .htaccess and how to change Apaches configuration using the same. – Mohd Belal Dec 29 '17 at 19:06
  • not rly, I already read this before and it didn't help me, so...you wouldn't call it a duplicate :) – Saltyy Dec 29 '17 at 22:48
  • what's the status of the question? if it was solved, then it'd be best to mark it as such, by accepting the answer. – Funk Forty Niner Jan 06 '18 at 01:56

1 Answers1

0

For rewriting the Path you need to include .htaccess file in your root directory to change Apaches configuration setting.

The .htaccess route with mod_rewrite

Add a file called .htaccess in your root folder, and see the below details:

This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to index.php:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

This enables the rewrite engine:

RewriteEngine on

This checks for existing folders (-d) and files (-f):

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

And this does the actual redirecting:

RewriteRule . index.php [L]

You can extend this to pass the requested path to the index.php file by modifying the RewriteRule to the following:

RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

The ^(.*)$ part tells the rewrite module that we want to pass down the whole requested path as one parameter. The QSA part tells the module to append any query strings to the request. The ?q=$1 tells the module how to pass down the parameter. In this case, it's passed down as the q parameter. You can extend this even further by using regular expressions. For example:

RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2

This will pass down the first part of the path as the first parameter, and the rest as the second. So the following request

http://yourhost.com/some/path/somewhere

will result in

http://yourhost.com/index.php?first=some&second=path/somewhere
Mohd Belal
  • 1,119
  • 11
  • 23