.htaccess to open php template file as requested URL - php

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.

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.

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.

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.

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]

Related

Php basic website page link

I'm creating a simple php website with changeable themes. I have a directory called MyThemes with a a folder inside containing the following files header.php, sidebar.php, page.php, footer.php now the problem is that i want to display a page from the database using the page.php file of the selected theme, but the generated link will be something like
website/MyThemes/ThemeName/page.php?id=somePageID
I want to change that if possible to something like
website/pages/somePageID
I have a little experience with PHP, but apparently not enough to do this. So any help will be greatly appreciated.
This is typically done via URL rewriting by the webserver: the server makes sure /url/like/this is converted to (for instance) something.php?like=this. After that there is no difference to the application. Apache uses mod_rewrite to do this. If you were using Django, this would be configured in the urls.py files.
You could still simplify your URLs to website/pages.php?id=n by remembering the theme in a cookie or session variable though.
Also, your current file paths suggest that you duplicated the pages for the different themes though: this is never a good idea. Don't repeat yourself!
You can use friendly urls and regular expressions:
See
http://www.phpriot.com/articles/search-engine-urls
In this article, you'll find instructions for using htaccess:
Example:
<IfModule mod_rewrite.c>
RewriteEngine on
#rule not apply directories
RewriteCond %{REQUEST_FILENAME} !-d
#rule not apply files
RewriteCond %{REQUEST_FILENAME} !-f
#Rule to page
RewriteRule ^page/$ page.php
RewriteRule ^page$ page.php
RewriteRule ^page/(.*)/(.*)?$ page.php?id=$1&des=$2
RewriteRule ^page/(.*)?$ page.php?id=$1
</IfModule>

Redirecting all php file and error document to index.php except request of stylesheet, images, java script, ajax

Well this question is quite common and i also searched and tried many answeres, but i didnt get a proper format that can solve my problems.
What i want.
Redirect all error document (ie any non found file or directory request should be redirected to root file which is in my case - index.php)
Redirect all page request to root file (index.php) as a parameter ie if
someone enter a url like
domain.com/directory_request/file_request
then it should be treated as
domain.com/index.php?directory=directory_request&file=file_request
and request like
domain.com/directory_request1/directory_request2/file_request
then it should be treated as
domain.com/index.php?directory=directory_request1-directory_request2&file=file_request
All images, css, javascript, file should not be redirected to root files.
One thing more i want all non-existing css, images etc should also be redirected to root file.
In short all requested url should be redirected to root file index.php with parameters as the request described earlier in point 2, except all EXISTING images javascript , css request.
i hope i clear my point.
Thanks in advance.
Usually, in cases like this, it's best to direct all requests to your front controller and parse the $_SERVER['REQUEST_URI'] inside of that file. This allows you to keep all of your rules and parsing in one place (as opposed to parsing URLs in .htaccess and routing requests in your front controller).
A simple rule like that could be (in your .htaccess file or httpd.conf):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
The RewriteCond is telling Apache to only run the RewriteRule if the requested filename is not an actual file
It's probably best to use the RewriteCond's to check if the requested directory/file exists.
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-f
And then you can use these in front of each of your rules. To get it exactly like you want you can use as many rules as you want like this:
RewriteRule ^(.*)/(.*)$ index.php?directory=$1&file=$2

PHP mod_rewire entire site, and rewriting image URLs?

I have pretty much developed an entire site and was foolish to not implement mod_rewrite. I wish to do so now, and did a test page. The mod_Rewrite works perfectly, but I then have the issue of all of the imaegs being linked to the new pages URL, for example:
www.mysite.com/county/Kent.html
from
www.mysite.com/sector.php?county=kent
It works great, but then when I am on the new link the URL for images changes to this.
mysite.com/county/images/widMot.png
As if the 'images' folder is in the 'county' folder which actually doesn;t exist. Is there a rewrite rule, which will ignore this image link change? Or something I am missing?
EDIT: As requested:
...
RewriteEngine On
RewriteRule ^county/([^/]*)\.html$ /sector.php?county=$1 [L]
...
EDIT: The solution the answer posted was excellent, and resolved the image issue, but I was then stuck with an issue of my css files being put after /images/ directory. Ofc a simple fix would be place them in there, but is there no other way?
Please take a look at the <base> HTML tag.
http://www.w3schools.com/tags/tag_base.asp
It allows you to define the base URL of your site, after that, any link built like /images/someimage.png will direct you to the root directory's images/.
EDIT:
To be more specific, all relative URLs starting with a forward slash / will begin immediately after domain name.
Try something like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule YOUR REWRITE RULE
</IfModule>
Should pass everything to your rewrite rule, unless the requested URL is an actual file on the server.

Some header passing variable?

Im trying to pass some variables from the URL to the PHP script. Now I know that www.site.com/index.php?link=HELLO would require $_GET['link'] to get the variable "link". I was hoping there are other ways to do this without the variable.
For instance I want a link structure like this: www.site.com/HELLO. In this example I know that I have to create a Directory called Hello place an index file and it should work but I don't want to create a directory and Im hoping there's a way to "catch" that extra part after the domain. I'm thinking of creating a custom HTTP 404 Page that will somehow get the variable of the not found page, but I don't know how to get the HTTP 404 error parameters. Is there another simpler way to get a variable without the use of the extra ?link= part? I just want it to be a structure like this www.site.com/HELLO.
What you want is URL rewriting. You don't mention what kind of web server you're using, so I'll assume it's Apache.
If you have mod_rewrite enabled on your web server, this can easily be accomplished by creating a .htaccess file in your document root with the following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1
This will make all requests - that match the regular expression [a-zA-Z0-9]+ - get forwarded to index.php. For instance, if you try to access domain.com/hello, PHP would interpret this as trying to access index.php?request=hello.
You can read more about this in the Apache HTTP Server manual about mod_rewrite.
In which case, you normally use .htaccess to alter the URL in some form. For instance:
RewriteEngine On #Enable Rewrite
RewriteCond %{REQUEST_FILENAME} !-f #If requested is not an existing file
RewriteCond %{REQUEST_FILENAME} !-d #If requested is not an existing directory
RewriteRule ^(.*)$ /index.php?link=$1 [L] #Preform Rewrite
Which will make www.site.com/hello to www.site.com/index.php?link=hello. This change is invisible to the user (he will still see www.site.com/hello as an address). Be advised that it may cause trouble if you try using relative paths with CSS/JavaScript files.
You need to use URL rewriting to do this. If you're using Apache: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
you can do that with a mod-rewrite structure.
Here's a pretty good tutorial on how to set it up with php/apache.
http://www.sitepoint.com/guide-url-rewriting/

.htaccess redirect if file doesn't exist

So here's what I'm trying to do. I have a simple framework and I am using mod rewrite to rewrite the urls so that they point to the correct files. I have a process folder which sits inside the web folder (the web folder is where all the actual website files like images, css, etc are).
This works great but what I am trying to do now is to have a default.php file to process the forms. The file works great if I point the form to it but I want to be able to point the form to it's proper file and if the file is not there then it uses the default.php
The forms are create by a class and the action is set to go to process/{ID OF THE FORM}.php The idea is that anyone can create a form using the class and if they don't need to do anything special, the default.php file will process the form.
I realize I can just set the form class to always use the default.php and then on the default.php file I can check if the proper file exits. If it does, then use that file, if it doesn't then keep processing the form but I am still curious to know if something like this would work on mod rewrite:
RewriteRule ^process/([^/]*) /web/process/$1
RewriteCond %{DOCUMENT_ROOT}/web/process/$1 !-f
RewriteCond %{DOCUMENT_ROOT}/web/process/$1 !-d
RewriteRule ^process/([^/]*) /web/process/default.php [L]
This rule alone should do it:
RewriteCond %{DOCUMENT_ROOT}web/$0 !-f
RewriteRule ^process/[^/]+$ web/process/default.php [L]
If the requested path cannot be mapped to an existing file, it should be rewritten to the default file.
You generally should have your RewriteCond calls before your RewriteRule. The Rule triggers if the Conds are met.

Categories