.htaccess redirect if file doesn't exist - php

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.

Related

Reuse PHP script with htaccess redirect

I have written a php script which lists all files and directories from the current directory where the php file is located. I want to use this script for many different directories and it would be great if I can reuse it for all of them. Is this possible without to put a new php file in all these folders? I think of something like htaccess redirect so if the user visits an URL to a specific folder the script is executed with the folder as parameter but it does not lie in the directory itself.
I hope you understand what I want and have any ideas for this?
Yes you need to make an .htaccess file.
So basically your code will reside in index.php which is in the root folder.
Now use the below code in .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
now when the user tries to access sub folders, the folder path will come as a parameter named "path" to index.php

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

.htaccess to open php template file as requested URL

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]

URL Rewriting and actual directory names matched by regex

I created an application using the Front Controller pattern, so basically everything is sent to index.php
This is what I currently have in my .htaccess file
RewriteRule ^([a-zA-Z0-9]+)(\/([a-zA-Z0-9]+))?(\/([a-zA-Z0-9\/]+))?(\/)?$
index.php?class=$1&method=$3&args=$5
(I had to break the line into two, for presentation but you know what I mean)
So it works fine however if I create a directory named /js for example that has to be directly accessible. That means, I can't put an .htaccess file inside it and put:
Deny from all
Then accessing my site using the URL http://mysite.com/js redirects to http://mysite.com/js/?class=js&method=foo&args=
Putting Options -Indexes in the main .htaccess file doesn't really help. Any thoughts?
Any clarifications are welcome, I'm not really an good at explaining things.
Thanks
Use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Exclude real files and directories from the redirection rule (redirection rule will not apply to the real files and directories).

Categories