How to check for mod_rewrite on PHP CGI - php

I am on shared host and PHP is inatalled as CGI script and that is all the problem i am not able to find whether mod_rewrite if enable or not
Note: I don't have any root level access so i can't much do with Shell.
I have tried the following :
1) checked in phpinfo() where i came to know about that this is the wrong place to look for in PHP-CGI.
2) I have tried getting it from apache_get_modules which agi does not work in PHP-CGI :(
3) I have tried :
if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) {
// mod_rewrite is enabled
}
which is asking for path to apache and i dont have this info SHELL cant reply to me and $_SERVER has nothing.
4) I have checked with RewriteEngine On in .htaccess and after this my site is throwing 500 Internal server error may be because of RewriteEngine is not there, but i need this is written to show someone.
Any body has any idea how to check get this DONE.
Thanks

With PHP CGI there is no easy way to find out whether mod_rewrite is available or not on the server.
So, you need to find out it by making a test call to the server. You can use the following steps for this method,
Create a directory named mod_rewrite_test on the server.
Add .htaccess file in this directory with the following code,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mod_rewrite_test
RewriteRule .* mod_rewrite_test.txt
</IfModule>
a. first line tells apache to execute the below code only if mod_rewrite is available,
b. second line enables rewrite engine,
c. third line sets rewrite base directory path which is from DOCUMENT_ROOT
d. forth line returns the content of mod_rewrite_test.txt file to any request sent to mod_rewrite_test directory.
Create a file named mod_rewrite_test.txt in the same directory. Write ok in this file and save it.
Now, using CURL in your php script, call the URL similar to,
http://www.your-domain.com/mod_rewrite_test/test.php
and check the response.
So, as per our .htaccess code, if mod_rewrite is active and working on the server,
it will return the content of mod_rewrite_test.txt file i.e. ok even though the test.php file does not exists.
Otherwise, it will return 404 - Page not found error.

To check if mod_rewrite module is enabaled, create a new php file in your root folder of your server. Enter the following
echo phpinfo();
And access file in your browser.

Related

PHP Routing on Apache Server

The following code, in index.php, gives the expected routing behaviour when I run it using the built in php web server:
<?php
$request = $_SERVER['REQUEST_URI'];
switch ($request) {
case '/' :
echo 'This is the Home Page';
break;
case '/about' :
echo ' This is the About Page';
break;
default:
echo 'Resource not Found (404)';
break;
}
The same code uploaded to my Ionos Linux web server running Apache does not work: If I access the root directory from a firefox browser (eg http://mywebsites.org.uk/root-directory) I get Resource not Found (404)in the browser. If I append something to the URL (eg http://mywebsites.org.uk/root-directory/about) the browser just gives some page with advertising on which is what you get when there is nothing corresponding to that path on the server). Same happens if you change 'about' for any other word.
I tried with the following .htaccess file in the root directory along with the index.php file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
Now I get the same result with http://mywebsites.org.uk/root-directory. That shows Resource not Found (404) in the browser as before. However, with http://mywebsites.org.uk/root-directory/about or any word substituting 'about' I now get an error message in the browser:
Multiple Choices
The document name you requested (/index.php) could not be found on this server. However, we found documents with names similar to the one you requested.
Available documents:
/index.html (common basename)
When I click on index.html (which is hyperlinked) I find that it is in the directory above the root-directory with the index.php file in it. Ie it is in the mywebsites.org.uk directory. Obviously it is not what I am after.
I also tried replacing the content of the .htaccess file with FallbackResource /index.php. The result was the same as when I had no .htaccess file at all which I described earlier.
I am finding it very difficult to understand my Ionos web server. I do not know if I can change configuration files for Apache or not and I have little understanding of .htaccess files. I have thought of changing to AWS over this issue as the documentation there seems more thorough. However I would prefer not to change if I can sort this issue out. Basically I want to be able to build a PHP router on my Ionos server which runs Apache.,
You are correct that the built-in PHP web server enables you to use a routing script. However, in environments such as your hosting on Ionos, this function is generally performed by whatever web server is running.
Unfortunately, I'm having trouble finding which web server Ionos offers by scanning their web site. Are you certain that they are running Apache and that they have mod_rewrite and local .htaccess overrides enabled? Any of these could explain why you're having issues there.
From your description of your issues, it also sounds like your web site may not have its document root path configured correctly, or that it is not set up to use index.php as a directory index file (e.g. via the DirectoryIndex Apache configuration setting).
The best I can suggest is contacting Ionos techical support or seeking out any technical documentation they offer to customers. I can't even tell if or what configurations they may allow you to override.

Mapping URL into custom files

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.
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>
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).
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.

What would cause Apache serving somepage.php/friendly-url yield 404 after PHP upgrade

My hoster has recently upgraded PHP to 5.3.8 and now all my installations that use a URL pattern like somefile.php/friendly-url yield a 404 ErrorDocument (where somefile.php is an actual existing PHP file which is supposed to handle the rest of the path). How do I best go about fixing this? What is the directive that tells Apache to serve said URL even if only the somefile.php part is valid but not the whole location does not really point to an existing file?
BTW: No URL rewriting is involved… Also, the 404 is clearly generated by Apache, not any scripts.
Try to add an .htaccess file to your public directory containing this line:
AcceptPathInfo On
From the PHP Manual:
Apache 2 users may use AcceptPathInfo On inside httpd.conf to define PATH_INFO.
Also have a look at the Apache2 documentation.
You probably had mod_rewrite configured differently before the upgrade... a similar thing happened to a sever I was working on about 2 months ago. Check out mod_rewrite and see if this is what you need.

Protect files in directory using authentication script in php/apache

I'm looking for a way to tell Apache that if there is a request for a file from a certain directory it should first run a php script to verify if the user is logged in.
I know I could put the directory outside of the docroot and let a php script handle the authentication and file downloads, but because these are flash files that try to open other flash files it has to be a directory in the docroot, and the files should not have to be send by the php script.
In the old setup we were using mod_auth_script(http://sourceforge.net/projects/mod-auth-script/), but as that is a rather obscure apache module I'd rather have a more common solution if possible.
You can use .htaccess and mod_rewrite to redirect requests to php script. Try some googling and you will find lots of examples.
.htaccess contents example:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ([0-9a-z-_]+)\.swf$ checkForAuth.php?&file=$1 [L]
This will call checkForAuth.php when someone will try to access *.swf file. In checkForAuth.php you need to check your session, read contents from $_GET['file'], set correct headers (content-type for flash) and output contents of requested SWF file.

What happens first? .htaccess or php code?

If I use mod_rewrite to control all my 301 redirects, does this happen before my page is served? so if I also have a bunch of redirect rules in a php script that runs on my page, will the .htaccess kick in first?
The .htaccess will kick in first. If you look at the Apache request cycle:
PHP is a response handler. mod_rewrite runs at URI translation, except for rewrite rules in .htaccess and <Directory> or <Location> blocks which run in the fixup phase. This is because Apache doesn't know which directory it's in (and thus which <Directory> or .htaccess to read) until after URI translation.
In response to to gabriel1836's question about the image, I grabbed it from the second slide of this presentation but it's originally from the book: Writing Apache Modules in Perl and C which I highly recommend.
When a request is made to the URI affected by the .htaccess file, then Apache will handle any rewrite rules before any of your PHP code executes.
Yes, the .htaccess file is parsed before your script is served.
.htaccess happens first.
htaccess is controlled by the webserver. This file will be taken in account before your PHP file.
For example, you could restrict access to a particular folder with your htaccess file. So, it have to be take in charge before your PHP.
Hope this helps.
The .htaccess is performed by Apache before the php script execution.
(imagine if the php script is executed and then the .htaccess make a redirection to another page...).
You always can test this with the following command:
wget -S --spider http://yourdomain.com
With this command you see the who is responding to your request.
As all the others mentioned, .htaccess is first.
So basically, the .htaccess more or less requires the relevant PHP code or files, as according to the rules specified in the .htaccess, meaning .htaccess is run first.

Categories