htaccess: rewriting with index parameter doesn't work properly - php

Alright, so I'm really struggling right now, I've tried everything, read articles and tutorials but I still don't know why this doesn't work.
I've got an index.php file:
echo "some test";
if(isset($para)) {
include $para . '.php';
} else {
echo "there's no parameter";
}
So if the user views the page with the parameter 'home' (http://mypage.com/test/index.php?para=home), he'll get the correct page. That does work.
.htaccess:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mypage.com$ [NC]
RewriteRule ^(.*)$ http://mypage.com/test/$1 [R=301,L]
RewriteBase /test/
RewriteRule ^([a-z]+)$ index.php?para=$1
But when I try to navigate using the rewritten link (http://mypage.com/test/home), the browser only outputs the included file and it completely ignores the index.php and its content.
Is there something wrong with my htaccess file? Or do I need to change some server configuration? Because I had a different htaccess file on a different webserver and it worked without any issues. But it didn't work on the webserver I'm using right now (sadly, I only have access to the htdocs folder).
EDIT: Just checked phpinfo, the page is running on PHP 5.3.28 on apache2.
Solved!
Problem solved. Adding 'Options -Multiviews' fixed the issue.

The hint here is:
The browser only outputs the included file and it completely ignores
the index.php and its content.
Try disabling mod_negotiation (Options -Multiviews) in the context where you want to do this remapping with mod_rewrite and within your script.
mod_negotiation is the most common reason "other" rewriting seems to be happening on your resources.

Related

server error using URI embedded images in a required file if index.php isn't in the url

Struggling. Help!
In index.php:
require "cachedPages/home.html";
If I visit: https://websiteaddress.org/index.php then it works fine.
If I visit: https://websiteaddress.org then I get an internal server error.
I guess it's a .htaccess thing. All I have in there is some cpanel php72 code and:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DirectoryIndex index.php
It seems there's some difference between how index.php is called if you call it directly as opposed to .htaccess calling it for you?!?
Any ideas?
Thanks in advance.
If you don't have one of the following options in your .htaccess then Apache won't know which file to default back to:
DirectoryIndex index.php
FallBackResource /index.php
Also I'd recommend installing mod_rewrite as well. It's handy for other reasons.
Well, I've managed to sort the problem, but it's a pretty weird situation that no-one else will probably ever experience. Here goes...
If the user lands at:
https://websiteaddress.org rather than https://websiteaddress.org/index.php
and that page (php) requires another page,
which has images that are embedded as URIs rather than linked src files.
Then, the first URI causes a server error.
If I replace the
img src='data:image/jpeg;base64,/998a9g98ahg...etc'
with
img src='path/to/file.jpg'
on the first instance of a jpg then it all works fine.
All the later URIs are fine, it's just the first instance!
It all works now, with this workaround; and the situation is so unique and bizarre that I doubt this thread will be of use to anyone else. In fact it's so edge-case that I can't be bothered investigating it any further myself.

Neither XAMPP or the live website is rendering the index.php automatically

Ever since I added (at least that's when I started to notice something's wrong) an .htaccess file to my root folder with the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
my website never landed on the index.php file automatically. To see the live result:
www.unidrones.co.za/ChoiceGelatin
The page is supposed to render the index.php file and automatically highlight the "HOME" link, like it does when I manually type index.php in the URL: www.unidrones.co.za/ChoiceGelatin/index.php
For one, it's reading the index file contents, but the CSS associated with it is not working. Each page has a body ID added dynamically, so the index file's body tag would have an ID of #index.
I've researched on SO and other forums, and seen similar problems, and I checked all as I tried them out:
edit the Apache config file (check)
remove the # comment from the allowOverrite All (check)
edit the htaccess file on cPanel (check)
delete the htaccess file (check)
place DirectoryIndex index.php in the .htaccess file (check)
this question was close but still does not apply get the index.php with .htaccess
some other solutions not worth mentioning (check all)
None of the above solutions work. I tried the site on XAMPP and live and neither is displaying as it should, like it always did until yesterday.
Add an index.php into your root with this content:
<?php
header('Location: ' . ('http://www.unidrones.co.za/ChoiceGelatin/index.php'));
?>
This will redirect you to the ChoiceGelatin's index page. The rewrite-rule in your .htaccess actually removes the burden of adding .php everytime you request for a php-file. It doesn't seem to be working though, probably because of the configuration in http.conf. Try enabling mod-rewrite!

PHP Permanent Redirection

I'm having a strange issue with a permanent redirection in PHP.
Here's the code I'm using:
if ($_SERVER['HTTP_HOST'] != 'www.mydomain.ca')
{
header("HTTP/1.1 301 Moved Permanently");
$loc = "http://www.mydomain.ca".$_SERVER['SCRIPT_NAME'];
header("Location: ".$loc);
exit;
}
So, the home page, referenced either by www.myolddomain.ca or www.myolddomain.ca/index.php both work but every other page on the site fails to redirect. I've spent a couple of hours looking at this from all the angles I know and can't fathom it. Does anyone have any idea what the issue could be?
As a note, I've tried this without the 301 header too and get the same issue.
If you have a ftp-account to the server, you can create a .htaccess file and let it handle the requests. Just create a file named .htaccess in the root-folder of your site and post the code, changed to your desired pattern(s)
Options +FollowSymLinks
RewriteEngine on
RewriteCond {HTTP_HOST} ^yourdomain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
If you want to do it fix via PHP, I would create a redirect.php and include it on every site you need it. Hard to tell if this is the best solution, it is a bit depending on your way of layouting and structuring.
Try it by htaccess, create .htaccess file and try something like this
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
** Apache Mod-Rewrite moduled should be enabled

Apache: mod_rewrite not directing one address

So I'm writing a web based program, and instead of having a mass of /?p=54&z=42, etc in the URL, I've set it up to direct the first "directory" (eg /home/) to be the function, the second "directory" (eg /home/view/) to be the action.
Right now, setting it as localhost/home/ works, setting it to localhost/settings/ also works, but when I change it to localhost/stats/ it tries looking for the file or folder... not redirecting it to a variable to be used in PHP...
I did have a .PHP file sitting there before but removed it 2 days ago. Apache is still trying to find the file that was there. I've restarted my computer a dozen times with no luck.
My .htaccess file looks like this:
RewriteRule ^([a-z]+)/([0-9,a-z]+)$ /$1/$2/ [R]
RewriteRule ^([a-z]+)/([0-9,a-z]+)/$ /index.php?function=$1&action=$2 [L]
RewriteRule ^([a-z]+)$ /$1/ [R]
RewriteRule ^([a-z]+)/$ /index.php?function=$1 [L]
What am I doing wrong, and what do I need to do to get this working?
In your httpd.conf or virtual host's conf file, you probably have an alias or something like that that makes it process /stats/ before the rules are reached.
Turns out there was a file called stats.deadphp in the root directory that was causing the issue. Deleting that cleared it up. :)

Apache Url Rewriting won't work

In the httpd.conf file I have AllowOverride FileInfo. In the .htaccess file in top level of my webserver with all the other files, I have this:
RewriteEngine On
RewriteRule ^downloads/?$ index.php?page=downloads [L,NC]
But it doesn't work. mywebsite/downloads and mywebsite/downloads/ always give a 404 not found. Any idea why? Thanks. (mywebsite/index.php?page=downloads does work).
And I'm restarting apache every time I change it.
And when I put the code above in httpd.conf, the website won't even load at all, just blank, spinning safari wheel forever.
Its fine if I just do RewriteEngine On, but if I do anything else (RewriteBase, RewriteRule), the web browser spend ages trying to load and finally giving this error:
Safari can’t open the page “http://mk12.gotdns.com/” because the server where this page is located isn’t responding.
Anyone have any idea what's wrong?
EDIT: I'm able to make, for example, css files forbidden with rewrite, and it works, but any rule that goes from downloads to index.php?page=downloads makes the server not respond (see above), it doesn't matter what page, the website won't load at all. Any ideas..?
The best way to debug rewrite rules is to enable rewrite logging so you can see what's going wrong.
It Worked!! I was putting the code in the wrong spot. It was in httpd.conf, but at the end. Move it into <Directory> and its good. Thanks for your help!
EDIT: Also, I found that It won't work if the flags are like this: [L, NC]. It has to be [L,NC] (no spaces).
So the two problems where that it wasn't inside <Directory "/Library/WebServer/Documents">, and there were spaces between the flags. Hopefully this will help someone else in the future.
Have you tired adding a slash in front of the "download" like below
RewriteRule ^/downloads/?$ index.php?page=downloads
EDIT: Try the code below:
RewriteEngine On
RewriteBase /
RewriteRule ^downloads/?$ /index.php?page=downloads
I would try removing the trailing slash and question mark after downloads, and the leading slash before index.php.
RewriteEngine On
RewriteBase /
RewriteRule ^downloads$ index.php?page=downloads.

Categories