htaccess causing index.php to download - php

I am working on a site that depends on the following htaccess file to create something of a virtual directory structure by redirecting to an index.php whenever the specified url doesn't exist on the system.
AddHandler php5-script .php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
That seems to work fine on the client's existing server. It invisibly redirects things like domain.com/checkout to domain.com/index.php?p=checkout
My problem is that on my local development environment, this causes any non-existing url like that domain.com/checkout, or even the root domain.com to DOWNLOAD the index.php
If I try to load a specific file that exists in the website such as domain.com/index.php then it parses and displays in the browser properly.
In case it matters, my development environment is configured with a Virtual Host on Zend Server Community Edition.

I suppose you've already configured PHP in your development server for all files with the *.php extension. That means that doing it again in your .htaccess files is not only unnecessary but it can possibly cancel your server's configuration (as it's actually doing). Thus you need to get rid of this line:
AddHandler php5-script .php
The question is why it's there in the first place. I'd expect any PHP-enabled hosting service to be configured by default :-?

Related

XAMPP Preview of PHP Redirects to Main Website

I recently had a third party contractor edit my website so you no longer see the ".php" at the end of each url. I am assuming he changed something with the .htaccess though I haven't been able to get a response from him since this job. Since then if I would like to preview my site for edits with XAMPP running the "localhost/filename" redirects me to my main online url. Is there anything I can change with with my xampp settings or .htaccess to allow me to view a preview for minor edits before uploading? I am not an expert in Apache or .htaccess files.
Thank you in advance.
EDIT Answer:
So this was not an .htaccess issue. It was a little more complicated than I thought. The easiest solution my colleague helped me through is as follows-
First I edited my host files using this tutorial: https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/?fbclid=IwAR3d5pnrWgOdutlcrtThAzpIQSTteESgi6p6eWoPLaE_IPN-C1jZegLJBBI
In my host file I just put
127.0.0.1 yourmodel-local.com.
Second, I opened up the Apache Config file "httpd.conf" In that file under "ServerName localhost:80" line I added this:
<VirtualHost yourmodel-local.com:80>
DocumentRoot "C:/xampp/htdocs/yourmodel/"
ServerName yourmodel-local.com
So Now with XAMPP running I can use the yourmodel-local.com as my preview url.
From what you are saying, there's probably a RewriteRule or a Redirect instruction inside the .htaccess.
Normally those instructions are used to use a specific domain for example always use the https version of www.site.com.
You should look for lines similar to these:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
If you could provide that part of your .htaccess file's code I might be able to indicate what you would need to modify.
UPDATE
After reviewing the .htaccess file you submitted, the problem is related to lines that get automatically added to your .htaccess from your hosting company's server. The lines that contain strings like .well-known/acme-challenge are used for SSL certificate generation.
Your remote .htaccess has these three lines in the beginning:
#RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.php [NC,L]
The # is used for comment meaning those lines aren't being used.
So, I think the easiest solution for you in this case would be to leave the remote .htaccess file as it is, but edit the one you have locally to just contain this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Basically you'll have only this in your local .htaccess and leave the remote as it is, because it also has some PHP configuration lines, which will probably cause a 500 error on your local development environment. Be sure to not include the original # symbols.

I have problem in .htaccess in my localhost not redirecting correctly

My .htaccess file contains:
Options +FollowSymlinks
RewriteEngine On
RewriteRule projects$ projects.php
RewriteRule projects/$ projects.php
RewriteRule projects/page/([^/]*)$ projects.php?page=$1
RewriteRule projects/page/([^/]*)/$ projects.php?page=$1
RewriteRule projects/company/([^/]*)/$ projects.php?companyid=$1
RewriteRule projects/company/([^/]*)$ projects.php?companyid=$1
RewriteRule projects/([^/]*)$ projects.php?id=$1 [L]
It was working fine, but suddenly this section is not working in my website.
This should redirect to "projects.php?companyid=$firstvariable",
but actually, I don't know why.
I tried restarting my PC and deleting temp and restarting WAMP.
After some interactive troubleshooting with OP, it appears that WAMP's Apache/PHP will execute a PHP file that matches the directory portion of the URL path if it exists, before applying the .htaccess RewriteRules.
In other words, if you have a file named Abc.php, and you request /Abc/def, WAMP's Apache/PHP will just execute Abc.php and not consult the .htaccess file.
I'm not sure if this is something special about WAMP and whether it's out-of-the-box behavior. I can't find any documentation on being able to control/change this behavior.

.htaccess file not rewriting URL's correctly when project is in subdirectory

I currently have a problem with rewriting my URL's using the .htaccess file when a Laravel project is in a subdirectory.
usually when not in subdirectory having /vacancies -> /index.php/vacancies using this below .htcaccess file works.
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Though now the project exists within a subdirectory I require the URL to be re written as so /vacancies -> /abc/index.php/vacancies.
The home page works correctly though any links just return a not found error.
What changes would I need to have the .htaccess file do this for me.
so on investigating further upon entering 'index.php' into the url itself the page loads correctly if this helps anymore with answering the question.
Thanks!
I wouldn't modify the default .htaccess provided in a Laravel app. You need to use Apache's directory alias in the virtual host config file for your site. You need to have the following format:
#Change the paths accordingly
Alias /vacancies /path/to/app/public
<Directory /path/to/app/public>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted #If using Apache 2.4 add this.
</Directory>
I managed to fix this thank you for your suggestions, This turned out to be a problem with the virtual host file and where the project was created as I have soft links let up to direct apache.
As such Apache was looking in the actual project location set in the virtual hosts file rather than the soft link one intended causing the not found issues.
Check of .ht* permissions in the apache httpd config file if you are using apache. I had the same problem and I had permissions denied problem for .htaccess file. If you are on shared host, try to contact hosting support.
Tried putting it on top of the htaccess file ? Helps for me in some kind of cases, seems weird but it does.
I notice fro your answer you've managed to "fix" this. However, you have some fundamental errors in the .htaccess file you posted, so I'm not sure how this is working exactly?
RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 3
These two directives are not valid in .htaccess files and consequently will result in a 500 Internal Server Error. These can only be used directly in the server config or virtual host context. These are also Apache 2.2 directives, so won't work on Apache 2.4.
RewriteRule ^index.php [L]
(Part of the "front-controller"). You are missing a space between the first and second arguments, so this will fail to rewrite any requests to index.php - your "front-controller". (Maybe this is just a typo, but it's difficult to see a typo like this could creep in?) For example, this should be more like:
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
These directives should be before the front-controller, not at the end of the file. Otherwise, they are not going to execute for any request that is routed through your framework.
when not in subdirectory having /vacancies -> /index.php/vacancies using this below .htcaccess file works
That's not actually what the above .htaccess code does. It should simply be rewriting the request to /index.php. (The frawework/Laravel then looks at the full URL that was requested.) If it rewrote the URL to /index.php/vacancies (that some frameworks do) then you would need to read the URL using pathname information (PATH_INFO).
Though now the project exists within a subdirectory I require the URL to be re written as so /vacancies -> /abc/index.php/vacancies.
See above regarding the PATH_INFO. But if the .htaccess file is located in the document root of the site and the /abc subdirectory should be entirely hidden then you still have some work to do. You'll need to set the appropriate RewriteBase and modify the condition that removes the trailing slash off non-directories.
Otherwise, if the /abc directory is part of the URL then the .htaccess file can be moved to the /abc subdirectory, but you'll still need to modify the directives that remove the trailing slash.

Codeigniter - getting 404 Not Found error

We have two hosting packages at godaddy. Our live website is working fine using following .htaccess file. Website is accessible without using index.php in url.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
We used same htaccess file and same code on other hosting that is also by godaddy. We just wanted to make production and development instances seperate. But on other hosting with same code and htaccess file, website shows 404 page not found error but website works fine with index.php in url.
We have same php version on both server. Just wants to know what could be the issue?
I also faced the similar problem when I move my 100% working code to dedicated Debian server. After 3 hours of research, i found out why htaccess file is not working.You need to allow overriding of htaccess in Apache Configuration.
Here are the steps.
Step 1:
Add this in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Step 2:
Remove index.php in codeigniter config
$config['index_page'] = '';
Step 3:
Allow overriding htaccess in Apache Configuration (Command)
sudo nano /etc/apache2/apache2.conf
and edit the file & change to
AllowOverride All
for www folder
Step 4:
Restart Apache (Command)
sudo /etc/init.d/apache2 restart
Before you make any changes in apache2.conf file make sure to make a backup copy of that file. If you are using Debian server you can avoid sudo command. I believe you know how to connect to your server using ssh client software like putty. Comment below if this method does not work for you.
Follow this
Create a new .htaccess file in your public_html/Project root
Paste the following code
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|resources|robots\.txt|static) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?$1 [L,QSA]
Optional
If you can ssh into the server using command ssh username#ip_address on a command line either Linux terminal or Putty
The username is the one given to you by GoDaddy or you can use root
The ip_address would be the Public IP to your host you can find it in your hosting panel
Run the command sudo a2dissite 000-default
The command disables the default apache config that handles request
Goodluck!
You can use this. This works for me in godaddy hosting.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?/$1
try this one.. it's works for me..
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
I have faced the similar problem when I move on godaddy server.And It's work for me.
Add this code into your .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
In config file:
$config['base_url'] = 'domain.com';
$config['index.php'] = '';
And Set AllowOverride to All in your httpd.conf
set the correct permission on the /cache folder, rather than just making the folder writable you need to make it reclusively writable.
I hope it will work for you.
Try to lowercase all your controllers. This fix the problem for me.
in index.php you echo "test" and run .then you can check whether it is called or not.
in config file enable error log .
Make your /application/logs folder writable.
In /application/config/config.php set. $config['log_threshold'] = 4;
check the log /application/logs folder
I have limited knowledge in server networking but this should give you a few ideas.
First ,you must know what is .htacess file is. you can check it here link
.htaccess is a configuration file for use on web servers running the
Apache Web Server software. When a .htaccess file is placed in a
directory which is in turn 'loaded via the Apache Web Server', then
the .htaccess file is detected and executed by the Apache Web Server
software. These .htaccess files can be used to alter the configuration
of the Apache Web Server software to enable/disable additional
functionality and features that the Apache Web Server software has to
offer. These facilities include basic redirect functionality, for
instance if a 404 file not found error occurs, or for more advanced
functions such as content password protection or image hot link
prevention.
Basically .htaccess is giving you permission to edit the configuration of Apache Web Server. You will gain permission using .htacess if mod_rewrite is enabled in Apache. I will not write what that 4 basic line in .htaccess can do in codeigniter because you can find it in google. this link can give you some nice basic info.
Now the main issue is .htaccess is not working in your other hosting. This is possible because a few reason :
For some reason, mod_rewrite is disabled in your Apache Web Server. If your hosting is VPS or DS it's possible you are didn't configuring it yet. Someone already ask how to enable mod_rewrite, you can go to this link. If you are using Shared Hosting, ask your hosting to enable mod.rewrite in your host. You can check if mod.rewrite enabled or not in your hosting following this link. Or you can simply check with <?php phpinfo(); ?> and check in "Loaded Modules" for mod_rewrite exist or not. Example in this image.
You are not using Apache as Web Server. There are others Web Server as NGINX and lighttpd. You should ask your server administrator what Web Server are you use currently. I dont have any experience using NGINX and lighttpd so i cannot give you any help in that. But as far as i know .htaccess code is different in APACHE, NGINX, and lighttpd. You can try to convert .htaccess apache code to nginx code in here or here. For converting .htaccess to lighttpd unfortunately, i cannot find any online converter but there are official guide here and here
Well for the complete tips sake i will write this. It's possible you are not configuring codeigniter to not use index.php. In config file, empty configuration for index_page. and give Base_url your domain name. $config['base_url'] = "yourdomainname.com"; $config['index_page'] = '';
I hope this clear your confussion why your .htaccess didnt work.
As you mentioned here, on localhost your code is working fine but when you moved it over live server it causes 404 error. There might be few cases you need to check:
Step 1: Check mod_rewrite is enabled or not in your Apache configuration
Step 2: Remove index.php in codeigniter config as #geordy suggested
You can check it by
<?php
echo 'Test'; die;
?>
in your root index.php. It ensures your all requests should go through this files else Codeigniter framework would not work properly.

Deploy Laravel on 1&1 Servers

I have recently completed my first laravel site, but now I am stuck with deployment. This is an entirely new concept for me. My webspace is supplied by 1&1.
I have attempted several tutorials, but none seem to work.
Based on the tutorial here at:
Uploading Laravel Project onto Web Server
I structured my server folders like so:
(note when first FTPing my server, there was no www or html_docs, only a logs folder).
In the www, my index.php was altered to:
require __DIR__.'/../laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/start.php';
In the laravel/bootstrap/paths.php file I altered:
'public' => __DIR__.'/../../www',
When visiting my domain in a browser, for example: myDomain.co.uk - the site is redirected to a "placeholder" (named /defaultsite) page for 1&1 domains and states "This domain name has just been registered."
If I append /www/index.php for example, I get file does not exist, even though the file is residing in my structure above.
As you probably noticed I am very new to server side aspects such as FTPing ect. I have read a few tuts, and all seem to take a different approach, leaving me confused to the best method.
I am not sure where to go from here, so any advice is appreciated. Thank you.
Edit:
I think I actually got it working, but now when I go to domain.com/public I get what I think is a DB error:
SQLSTATE[HY000] [2002] No such file or directory.
Any Advice please?
The reason you are seeing /public/index.php is because you are pointing to the top level directory and not the /public directory. To fix this go to:
Manage domains
Expand your domain and click "Edit Destination"
Change the destination directory to /public/.
This should remove public from the URL.
This is the .htaccess file I use for 1and1 and laravel 4.2 and it removes .index.php and also uses an updated version of PHP:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
AddHandler x-mapp-php6 .php
AddType x-mapp-php6 .php
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]
</IfModule>
All fixed :) turns out 1&1 do not host their db name as local host, or 121.0.0.1. I simply changed the details and works a dream. next step is to get rid of the terrible /public/index url.

Categories