2

I'm trying to figure out a way to load a template php file AS a requested url/filename:

I have a directory with hundreds of pages, and would like to be able dynamically generate them from a template file rather than have hundreds of files containing the same code. Ideally there would be one php file that loads content from MySQL using basename(), where every url requested from a particular directory would open /gallery/template.php AS the requested url, such as /gallery/example.html.

I feel like this can probably be done using .htaccess and mod-rewrite, but I haven't found an example of it in action. I'm trying to avoid using GET, but if there is a better way to achieve this effect, I'm open to suggestions. Thank you.

  • Perhaps mod-rewrite is not the right strategy to achieve my goal? I'm basically trying to use a php file as a template to dynamically generate other urls, similar to what wordpress might do: I don't want "example.html" to physically exist on the server, but rather when someone requests"example.html" it will be generated from "template.php". – user1854528 Nov 26 '12 at 21:33

4 Answers4

0

Basically, you are wanting to execute a front controller pattern. If everything in your gallery subdirectory needs to be redirected (i.e. there are no images files or subdirectories which don't need to be routed to template.php), you can do a simple RewriteRule to achieve this. Just place this in .htaccess or your apache .conf file

RewriteEngine On
RewriteRule ^/?gallery/.*\.html$ /gallery/template.php [QSA,L]
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • Thanks for your response. I tried your code, but I didn't seem to have any effect. I'll try to clarify my situation a bit: The .htaccess file will be placed in the /gallery/ directory, and there is a subdirectory containing images, so only the "hypothetical" html files in the directory will need to open as template.php. – user1854528 Nov 26 '12 at 20:51
  • I have changed my answer to reflect only redirecting /gallery/*.html files. Have you checked you Apach .conf file to make sure AllowOverride is On for this directory? – Mike Brant Nov 26 '12 at 20:58
  • I tried this out and am now getting a "no input file specified" error. – user1854528 Nov 26 '12 at 21:08
  • @user1854528 Is that good? Is that an error in your PHP script? – Mike Brant Nov 26 '12 at 21:53
  • I don't believe it's functioning properly, because the PHP script works when the template file is directly opened. Thank you for you help, I believe I'm going to abandon this approach for one that I know will work - namely, creating all of the pages individually with identical include files. – user1854528 Nov 26 '12 at 22:09
  • Well something is sending you a "no input file specified" error. that doesn't sound like an Apache error bu rather a PHP one. In fact if there was an error in the Apache rewrite you would either get a 500 server configuration error or simply a 404 error if the redirect went to a non-existent resource. – Mike Brant Nov 26 '12 at 22:11
0

I haven't test it, but here is an example:

RewriteEngine On
RewriteRule ^gallery/.*/?$ gallery/template.php [NC,L]

String 'gallery' must be in the requested URL.

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • Thanks for your response. This code gave me an internal server error, when I placed it in the /gallery/ directory. – user1854528 Nov 26 '12 at 20:52
  • You have to put .htaccess in the directory above "gallery" and delete any other .htaccess files in the path. – Felipe Alameda A Nov 26 '12 at 20:54
  • I tested it again with your suggestions, but it doesn't seem to be doing anything. – user1854528 Nov 26 '12 at 21:09
  • Which theoretically means it's working otherwise it would have thrown an error. ¿Is template.php at gallery/ director? – Felipe Alameda A Nov 26 '12 at 21:14
  • Yes, the template.php is in the gallery/ directory. However, now when I try to request an html page in the gallery/ directory, I receive a 404 error. – user1854528 Nov 26 '12 at 21:19
  • Of course. The regex is implemented to filter all requests to "gallery". It can be modified, though, but the important thing now is to get it running. ¿What does template.php do? – Felipe Alameda A Nov 26 '12 at 21:21
  • template.php generates the html code for the individual pages. However the pages need to retain their url, because the unique file name of each page is used a variable within the document. That's why I'm trying to find a way to open the "template.php" file AS the requested file name. – user1854528 Nov 26 '12 at 21:27
  • @user1854528 OK. If I understood right, template.php requires some parameters from the original requested URL to generate the html code, which are not in the modified URL. It can be done, but not the way you are thinking. The requested URL must carry more nformation. That, if I understood right, repeat. – Felipe Alameda A Nov 26 '12 at 21:36
  • You are correct. If that's the case, I think I'll abandon this idea, and pursue a different strategy instead. Thank you. – user1854528 Nov 26 '12 at 21:47
  • I think the way is actually implemented is good enough and hard to improve with redirection. – Felipe Alameda A Nov 26 '12 at 21:52
0

You might want to use AJAX as a method for accomplishing what you want. Are you familiar with AJAX? If you post more details about your specifics and desired outcome, perhaps we can help you further.

Review these simple AJAX examples, and see this video resource.

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Ajax might work to achieve in my goal in the end, however, I'm trying to come up with a solution that isn't dependent on Javascript. – user1854528 Nov 26 '12 at 21:24
0

Take a look at $_SERVER['PATH_INFO'] on the $_REQUEST manual page. Using that, you can get something like

http://example.com/index.php/path/to/your/template

to call index.php passing along the entire /path/to/your/template as a string in $_REQUEST['PATH_INFO'].

And in your .htaccess file, you can do the following:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

(rewrite rules taken from here. I haven't done the mod-rewrite part myself, so I can't guarantee this will work).

This will take something like http://example.com/path/to/your/template and have the server treat it as http://example.com/index.php/path/to/your/template, making it pass everything to your index.php file.

After that, how you load your templates is up to you.

Tarka
  • 4,041
  • 2
  • 22
  • 33
  • I'll look into this, I'm not sure if it's exactly what I'm looking for, but it seems to be in the right direction. – user1854528 Nov 26 '12 at 21:22