.htaccess not working in linux server - php

Actually my project is in WordPress. I'm changing my server from windows to Linux.
After changing this I'm facing this error!
Why isn't my .htaccess supporting the Linux server?

If you're using Apache, you should configure it in order to allow .htaccess files to be executed.
To do so, you can
Create a virtual host, that configures specifically one site. Using a virtual host you can set a domain name, document root, server alias, etc. Also, you can set Allowoverride to All (or other). See AllowOverride
Set your configuration in /etc/apache2/apache2.conf (or /etc/apache2/httpd.conf in some versions). In these files, there is a <Directory "/var/www/" that points to your /var/www/ directory. Inside this Directory tag, you can set AllowOverride to All. Using this configuration, every site on your server will be allowed to use .htaccess.
I recommend to use the Virtual Host that allows a easier and cleaner configuration.

Checkout the comment in .htaccess file, if it starts with // then change it to #
For me this solved the problem, I wish for you too.

Related

How to set Apache to accept override from .htaccess

I have tried to run php in .html file and failed despite trying all solution suggested here. Someone suggested it maybe because Apache is not setup to accept .htaccess overrides. Maybe that is the reason. Now the question is how do I setup Apache?
To be able to use .htaccess files, the directive AllowOverride must be set for the directory into which you want to put .htaccess files. E.g.:
<Directory "/usr/local/httpd/htdocs">
AllowOverride All
</Directory>
This must be set in the core httpd.conf file of Apache. If you're on a shared host, you likely have no access to that (and that's correct, for security reasons).

WAMP - Remove localhost from project URL

The URLs of my projects in WAMP are not resolving as I'd expect. For example, I'd expect the project in the folder c:\wamp\www\project1 to have the URL http://project1/, but it actually has the URL http://localhost/project1/.
This can cause problems when accessing server variables. How do I fix this?
You can also look at this answer specially if you are now using WAMPServer 3 or greater, for a simple clikc and go way to create Virtual hosts.
Actually this change was intended by the WAMPServer developers and for a good reason.
There is a problem using the localhost/project1 url and the default WAMPServer DocumentRoot in that it causes problems for some frameworks and WordPress type environments, as well as your own code if you are using code which depends on knowing anything about the server environment.
The correct solution is to create Virtual Hosts for all your projects even those that you store in the \wamp\www\project1 style folders.
When doing that the DocumentRoot is \wamp\www and that is what causes these problems.
These tools expect the DocumentRoot to be the root of the site i.e. \wamp\www\project1 so that when they use PHP variables like
$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME']
$_SERVER['DOCUMENT_ROOT']
they get the correct answer i.e. the answer they would get on a real live server hosting just that site.
So using the localhost\project1 style url would mean these variables would return
$_SERVER['HTTP_HOST'] = localhost
$_SERVER['SERVER_NAME'] = localhost
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www
When they should return
$_SERVER['HTTP_HOST'] = project1
$_SERVER['SERVER_NAME'] = project1
$_SERVER['DOCUMENT_ROOT'] = C:/wamp/www/project1
So what you should do to make the My Projects menu work and reduce your pain in copying sites to live servers is:
Create an entry in the HOSTS file for each project like so and remember to create one for access via IPV4(127.0.0.1) and one for access via IPV6 (::1):-
127.0.0.1 localhost
127.0.0.1 project1
::1 localhost
::1 project1
Remember to refresh the Windows DNS Cache after any change to this file like so :-
Start a command window using Run as Administrator and run :-
net stop Dnscache
net start Dnscache
Now you must create a Virtual Host definition, so edit the \wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conf file ( apache versions may differ )
Delete the default stuff in there the first time you do this. And then create your Virtual Host definitions like so :-
#
# Use name-based virtual hosting.
# This next line is not required if you are using Apache 2.4.x and should be deleted
NameVirtualHost *:80
## should be first so the wamp menu page loads and is the default site
## should also never be changed from only allowing access from the local machine
## for a bit of extra security from casual ip address probing
<VirtualHost *:80>
DocumentRoot "C:/wamp/www"
ServerName localhost
ServerAlias localhost
<Directory "C:/wamp/www">
AllowOverride All
<IfDefine APACHE24>
Require local
</IfDefine>
<IfDefine !APACHE24>
Order Deny,Allow
Deny from all
Allow from 127.0.0.1 localhost ::1
</IfDefine>
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/project1"
ServerName project1
ServerAlias project1
<Directory "C:/wamp/www/project1">
AllowOverride All
<IfDefine APACHE24>
Require local
</IfDefine>
<IfDefine !APACHE24>
Order Deny,Allow
Deny from all
Allow from 127.0.0.1 localhost ::1
</IfDefine>
</Directory>
</VirtualHost>
Now you need one more change, you must uncomment the line in httpd.conf that includes the above file we have just changed. So edit the httpd.conf file, use the wampmanager menus to do this as it ensures you edit the correct file.
Find this line #Include conf/extra/httpd-vhosts.conf and remove the comment # symbol from the beginning of the line like so :-
Include conf/extra/httpd-vhosts.conf
Now of course you will need to restart Apache so that it picks up your configuration changes.
If Apache does not restart, you probably made a mistake in the config, to find out what is wrong try this.
Open a command window and CD into the \wamp\bin\apache\apache2.4.9\bin folder.
Then run this :-
httpd -t
If the error is in httpd.conf or the httpd-vhost.conf files it will tell you the error and also give you the line number to make finding the error very easy.
in your www folder open index.php
at line 30 change:
$suppress_localhost to be false
this is should look:
$suppress_localhost = false;
In your www folder open index.php at line 30. Here, change $suppress_localhost to be false. So, it is should look:
$suppress_localhost = false;
That was the quickest and easiest fix for me. I'm using 64 bit Wamp.
Okay, I had this problem. So, I troubleshooted the problem and traced it to an actual solution, NOT A HACK.
The SOLUTION :
Right-Click WAMP-server icon
Select WAMP Settings,
Select (enable) option Add localhost in URL
DONE. The remainder is FYI of how and why.
Note: localhost/myproject.php or myproject.php. Although the solution was already accepted, I saw some posts that got me confused. The accepted solution is based on a single project wrt different server applications, based on the OP's specific question, and how to influence that single project in question. But all the other solutions are hacks and don't really answer the solution to the OP's question, but do bring up a good point about the URL. So, according to the other "solutions", here is how to toggle the localhost reference in the URL. Hence my additional solution added to the mix.
This is a toggle switch.
Troubleshooting Process (no hacking involved):
Let's peek at the index.php
Let's look at the config file. Note the variables and Array?
Here is the array. A variable used earlier. Let's see... Oh, it tells us where and what to do.
As noted in the SOLUTION:
Your wamp seems to be configured to run a website on the normally non-existant domain helloworld.
add:
127.0.0.1 helloworld
inside this file: c:\windows\system32\drivers\etc\hosts
Make sure you start your text editor with administrator privileges to be able to edit that file.
This will tell your computer that the otherwise non-existant domain helloworld should be resolved to your loop back address.
For me was the easiest way go to http://localhost and in wampserver homepage use Add a Virtual Host (Tools section). There is nice and easy form to create alias without any problem (instead console when you using tray icon to create alias). No source edit, just using what wamp offers. Remember refresh DNS after creating of alias. Tested on Win10, WampServer 3.0.6 64bit.
Create a virtualhost like RiggsFolly said.
And try to uncomment LoadModule rewrite_module modules/mod_rewrite.so in httpd.conf
HostnameLookups ON not OFF in httpd.conf with DocumentRoot changed or not.
tested in browser for
$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME']
$_SERVER['DOCUMENT_ROOT']
C:\wamp\www
In index.php
line 338
($suppress_localhost ? 'http://' : '')
change http:// to http://localhost/
To do this you can create a virtual host using Add a virtual Host utility under Tools menu on localhost's homepage.
For more info on how to create a virtual host visit : Step by step instructions
I think that the easiest and quickest way is to:
Open index.php in your www folder >>> change: $suppress_localhost to
be false / no.
This isn't really an answer per se. It seems that the quickest way of removing a Virtual Host using WAMP is either not create one in the first place or be prepared to uninstall/reinstall it. What is the path to the config file to correct a errant and otherwise not malfunctioning WAMP Server?
If your "Your Projects" folder exists in "wamp/www/" and if you can see the localhost home page after starting wampserever correctly, and still you can't access your projects, then simply go to "wamp/www/" folder, open index.php and search for $suppress_localhost and set its value to false. Restart wampserver, go to localhost and try to access your project.
i also faced same problem after installing new wamp setup on window 7, 64bit.
just change line no. 30
$suppress_localhost = false;
Its work for me.
Open index.php in www folder and set
$suppress_localhost = True;===>$suppress_localhost = false;
that is work.
You can update "urlAddLocalhost" variable in "wamp64/wampmanager.conf" file to on/off. By default it is "off".
My wamp version is 3.0.6.
urlAddLocalhost = "off"

Running PHP file outside of documentroot (cgi-bin folder)

I am working with a colleague to set up their local environment on a MAC in XAMPP, on windows my vhost looks like the one below.
When I access a URL like http://domain.local/cgi-bin/handler.php the web server processes the PHP correctly but on his we get a 500 server error and this message...
The server encountered an internal error and was unable to complete your request.
Error message:
Premature end of script headers:
We tried changing the name of the cgi-bin folder to something else as I noticed there was another alias in httpd.conf but this had no effect...so it seems to me like the issue is permissions related.
We believe the alias is setup ok as accessing http://domain.local/cgi-bin/filenothere.php which doesn't exist throws a 404 as expected, any .html/.pl files fail to execute.
The permissions that exist on the cgi-bin folder are...
rwxrwxrwx dave staff
and is owned by the user and group....
dave staff
Vhost
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName www.domain.local
ServerAlias domain.local
ServerAlias api.domain.local
# Indexes + Directory Root.
DirectoryIndex index.php
DocumentRoot E:/home/www/www.domain.co.uk/htdocs/
# CGI Directory
ScriptAlias /cgi-bin/ E:/home/www/www.domain.co.uk/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
# Logfiles
ErrorLog E:/home/www/www.domain.co.uk/logs/error.log
CustomLog E:/home/www/www.domain.co.uk/logs/access.log combined
</VirtualHost>
Any idea what is causing this PHP file to not be executed?
UPDATE
Tried adding a header to say that the content is PHP to the start of the PHP file and this now simply outputs the PHP code.
It's as if any path specified in as an Alias is accessible but the server doesn't know how to execute the content, this is for HTML as well as PHP
I think you need a section for your cgi-bin.
The fact that your server can show you the script sources means the server has at least read permissions on /file/system/path/to/cgi-bin and IIRC that clashes with ScriptAlias b/c ScriptAlias expects /file/system/path/to/cgi-bin to be unaccessible for security reasons. I think your solution should look something along the lines of:
<Directory /file/system/path/to/cgi-bin>
Options ExecCGI
SetHandler cgi-script
</Directory
There is (very) rarely a need to run PHP scripts as CGIs given that the PHP module for Apache can execute them directly. Try adding this to your Apache config:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Afterwards simply place the PHP scripts into the document root for the site and see if they work. You'll want to remove the /cgi-bin/ part of the URL.
You say you're setting XAMMP on a Mac, but you have a drive letter (E:) prefixing your paths. OS X does not have drive letters like Windows, and this may also be causing (part of) your issue.
I don't know much about settings used. But I think you should go through following links. Might get some help.
http://www.sean-barton.co.uk/2009/02/setting-up-a-phpmysql-local-development-environment-on-a-mac-doing-it-properly/
http://docs.joomlabamboo.com/using-joomla/setting-up-a-quick-start-package-on-your-local-server-xampp-pc
http://www.adobe.com/devnet/dreamweaver/articles/setup_php.html
How to create Mac OS X dev environment for legacy PHP app?
Assuming that PHP is running in Safe Mode you may need to "open" your cgi-bin directory, as the execution of user (PHP) scripts is limited to the DocumentRoot and it's subfolders.
For all I know you could do that in two ways
1. Edit your php.ini
Locate the line containing open_basedir. If there's a comment at the beginning of the line - a semicolon - remove it. Then add your cgi-bin directory.
open_basedir = "E:\home\www\www.domain.co.uk\cgi-bin\"
If you need to open more than one directories you can use semicolon ; as a separator. On Linux based server, use a colon :
open_basedir = "E:\home\www\www.domain.co.uk\cgi-bin\;E:\home\www\www.domain.co.uk\another_dir\"
In cases like mine, where your server is hosted by third party, you'd need the second option (well sort of)
2. Edit your VirtualHost
Add the following to your VirtualHost, i.e. after DocumentRoot:
php_admin_value open_basedir "E:\home\www\www.domain.co.uk\cgi-bin\"
Same rules apply here for multiple directories and difference between Linux and Windows systems as above.
Hope that helps
Do you know whether PHP is running as a CGI program or as a webserver module? You should be able to find this out if you can get a phpinfo() page working (maybe from a regular folder inside the website root). If you're running as a webserver module then you should have a section near the top with a heading of Server API which says Apache 2.0 Handler (or equivalent).
From these pages:
https://bugs.php.net/bug.php?id=13316
http://php.net/manual/en/install.unix.commandline.php
http://gallery.menalto.com/node/8955
... it seems that it may be either due to PHP running as a CGI script, or else a conflict between PHP and another CGI handler.
One of the posters on the third linked page found that their similar-sounding end of script headers issue was resolved by removing / commenting out the Options +ExecCGI line in their .htconfig / vhosts file.
Might be worth having a read through the above links to see if your problem is related.

403 Forbidden on symlink in web root

I am on a shared hosting package on a LAMP stack with no shell access.
I can create symlinks using PHP's symlink() function.
Let's say my web root is /home/www/user1/public
Let's say I have a real directory named /home/www/user1/public/real_dir
And I create a symlink named /home/www/user1/public/fake_dir pointing to real_dir
Why would I get a 403 Forbidden when trying to access www.mydomain.com/fake_dir but not when trying to access www.mydomain.com/real_dir?
It shouldn't be a rights problem because when I create a file in PHP, I can access that all right.
I tried switching FollowSymlinks off and on in .htaccess (it was on), but no luck.
Could it be that FollowSymlinks is defined as not overwritable in a .htaccess file? Or is there something else to be aware of when working with Symlinks in Apache?
Apache has to be configured to allow access to the directory on the filesystem. This has to be done by a system administrator by inserting a <Directory> directive in the apache configuration files (httpd.conf).
Since the real directory is inside the web root it must be accessible, but FollowSymLinks may not have been enabled for the directory - this also has to be added to the <Directory> directive.
See http://httpd.apache.org/docs/2.0/mod/core.html#directory
This is possible SELinux security issue.
cat /selinux/enforce
if the value is 1, set it to 0, then restart apache.

phpmyadmin security

I have a production server with apache2, php, mysql.
I have just one site right now (mysite.com) as a virtual host. I want to put phpmyadmin, webalizer, and maybe webmin on there. So far, I installed phpmyadmin, and it works but the whole internet can go to mysite.com/phpmyadmin
How can I reduce the visibility to say 192.168.0.0/16 so it's just accessible to machines behind my firewall?
1) You can do it at the Webserver level.
Use allow/deny rules for apache. If you don't have direct access to your apache configuration file, you may use a .htaccess file.
<Directory /docroot>
Order Deny,Allow
Deny from all
Allow from 10.1.2.3
</Directory>
2) You can do it at the application level using the phpmyadmin config file.
The configuration parameter is: $cfg['Servers'][$i]['AllowDeny']['rules']
Examples of rules are:
'all' -> 0.0.0.0/0
'localhost' -> 127.0.0.1/8
'localnetA' -> SERVER_ADDRESS/8
'localnetB' -> SERVER_ADDRESS/16
'localnetC' -> SERVER_ADDRESS/24
You can see this on the official phpMyAdmin configuration documentation.
http://www.phpmyadmin.net/documentation/#servers_allowdeny_order
You would use a module in Apache called mod_access
You can either configure it in your apache config file or within a .htaccess file in the directory's root.
Here's a short example
<Directory /your_folder/location>
Order Deny,Allow
Deny from all
Allow from 123.123.123.123
</Directory>
Use the <Location> directive (either in server configuration or if it is allowed, in .htaccess). In there, you can use Allow from to deny access to everyone else except some certain source.

Categories