0

I want to map URL in my localhost XAMPP into custom files.

For example:

  • localhost/index.php --> d:\xampp\htdocs\index.php (default)
  • localhost/normal/data.php --> d:\xampp\htdocs\normal\data.php (default)
  • localhost/view/userinfo.php --> d:\xampp\htdocs\view.php?p=userinfo (custom)
  • localhost/view/welcome.php --> d:\xampp\htdocs\view.php?p=welcome (custom)

So, basically, all URL that goes into inside view path will be mapped to view.php files with the filename.php (minus the .php) as its query parameter. There's actually no physical folder view, and no physical files userinfo.php and welcome.php inside the folder.

The reason that I need to do this is that so I can pass all the pages that viewing data into an "application frame" that will wrap the page with header, menu, and footer, and I don't need to give header, menu, and footer call in each page. I might have the actual files userinfo.php that I can $include_once, or I might not (I can just generate it from within the view.php), but hey, that's one of the power of this kind of framework, right? And, if someday I need to change this structure, I can change it from just within one file (view.php), not all.

Can I do this in PHP and XAMPP? How? I've noticed that some website seems to used this practice (URL which have no actual files or even path at all), but when I try to read tutorial for it, I got confused.

URL mapping in PHP?

The accepted answer listed 3 links to learn about URL rewriting. Mostly they're written for Apache in Linux, and mostly they pull all the possible scenario and configuration that I got confused which one I really need with all those long documents and technical jargon. I need just the practical step of my specific problem, and then, I will be able to start from there to explore myself if I have more advanced needs. Please help.

Community
  • 1
  • 1
Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124

3 Answers3

1

XAMPP uses apache so the rewrites would work the same in Windows as they do in Linux. You could place a .htaccess in the site root directory with some rewrite rules.

However, using PHP

in d:\xampp\htdocs\view\userinfo.php you could include the line

<?php 
  header('Location: http://localhost/view.php?p=userinfo');
?>

But this must be before any thing is echoed to the screen (even whitespace).

Mark Fee
  • 122
  • 1
  • 11
  • I understand your solution, and that was my original solution that I've think of. But I feel that is a workaround and it's not neat to develop site like that as it can confuse the future developers. Thanks for your answer! :) – Chen Li Yong Sep 08 '14 at 03:37
1

You can use the Apache module mod_rewrite to edit requests before they hit PHP. You want to put something like the following in a .htaccess file in your htdocs directory.

RewriteEngine on
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/(.*)\.php.*$ view.php?p=$1 [L,QSA]

QSA means Query String Append. This means that if there are any GET parameters set on the original request they will be appended to the end of the new request too.

Note that this assumes that Apache is configured with AllowOverride enabled and the mod_rewrite module loaded.

gandaliter
  • 9,863
  • 1
  • 16
  • 23
  • Thanks for your information. All of these 3 answers are complement each other for my case, and too bad I can't choose all these 3 answers as the accepted answer. :) – Chen Li Yong Sep 08 '14 at 03:36
1

if you do want to go down the mod rewrite route adding the following to an .htaccess file in the site root should do it. You will need to make sure mod rewrite is on for XAMPP and I can't help you there I'm afraid. As you can see it rewrites the url, not the windows filename - so it would work on any OS.

The ([a-z]*) means it will take any filename.php with lowercase letters and redirect to /view.php?p=$1 where the $1 will be replaced by filename.

the [L,R] (L means last rule so stop processing if any more are reached, and the R means redirect (it will change the url in the browser). Use P instead to reverse Proxy (the user will still see the url they requested but the server will serve the correct file) - This will require mod_proxy as well.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_URI} ^view/
  RewriteRule ^view/([a-z]*).php$ /view.php?p=$1 [L,R]
</IfModule>
Mark Fee
  • 122
  • 1
  • 11
  • Thanks for the information about the [L,R]. As I see it, L is last rule tag thing, and I think I will use it in conjunction with P (reverse proxy). But I also concern with @gandaliter's answer, [L,QSA]. I just realized that I also need to forward the rest of query string to the view.php page. How can I combine [L,P] with [L,QSA] if both are last rules? – Chen Li Yong Sep 08 '14 at 03:35
  • 1
    [L,R,QSA] should work fine http://httpd.apache.org/docs/2.2/rewrite/flags.html explains further – Mark Fee Sep 08 '14 at 05:24