How to activate mod_rewrite? - php

I know that this question have been asked several times. But I can't get it to work.
I installed Apache2 in my Ubuntu server I can also confirm that mod_rewrite is installed using phpinfo();. I have also put a file called .htaccess in my root folder(/var/www/.htaccess). In my .htaccess file I paste the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^.*$ test.php
So everything is redirected to test.php
But it still doesn't work. So I checked my httpd.conf file under /etc/apache2. It is completely empty, with no lines of code (This seems a little odd to me)?! However checking in Stackoverflow answers there should be at least the following code in httpd.conf:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>
So I paste that code in httpd.conf. And restarted Apache with sudo /etc/init.d/apache restart. And it still does not work?
I have also tested to open the file /sites-enabled/000-default and /sites-available/default, where all virtual host settings lies and change under the directory /var/www and / to AllowOverride All. The mod_rewrite still doesn't work. Can anyone please help me. This problem has been baking my nuts for a while.
Also, my apache2.conf file doesn't work. I tried to write som nonsense. And it is still gives me the normal result instead of http 500 error

if you run this command,
sudo a2enmod rewrite
ubuntu will output whethere it is activated or already running.

All suggestions from Niels Bom are the smartest ones (I think). But I would add this as the first suggestion: try to launch and stop apache via the Ubuntu command: then when it's supposed to be stopped, make sure it's stopped ie verify your local page doesn't show anymore.
read what's in the folder /etc/apache2/. There should be apache2.conf conf.d envvars httpd.conf mods-available mods-enabled ports.conf sites-available sites-enabled
then if everything is ok, take a look at mods-enabled where you will find if the mods are enabled if so, then you can go on, otherwise take a look at the other answers about enabling modrewrite
take a look at sites-enabled: it's where you can find the sites that are... (guess what?) enabled. There should be the default website.
if so, open this file, and try to put apache nonsense in it, restart the server and see if it's happy or not. If it's not happy that's good. If it's happy then you're working on the wrong file
last: try to check what are the authorisations for htaccess files for this default vhost. Here's the way they work: if Apache doesn't find any directive in the vhost file, it will apply the global configuration. So I'd suggest to add the configuration in your vhost file, between the <virtualhost> - </virtualhost> tags (of course).
Please tell me what's going on and I'd be glad to update my question accordingly.

Because this thread ranks very high on google, here another solution:
WHAT'S CAUSING THE PROBLEM ?
It's caused because the standard definition in apache simply blocks all .htaccess stuff. Seriouslsy. This seems to be the initial state in Ubuntu 12.04 LTS.
SOLUTION
In /etc/apache2/sites-available/default (that's a file, not a folder) there's a code block like
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride XXXXXXXXXXXXXXXXXXX
Order allow,deny
allow from all
</Directory>
Here you should change
AllowOverride None
to
AllowOverride All
IMPORTANT
You have to do these changes with sudo rights, anotherwise Ubuntu will not save the file. After a RESTART your .htaccess stuff should work. To test .htaccess simply put some nonsense text into it and surf to localhost or 127.0.0.1 -> If you see "internal server error": voila! Remove the nonsense and you are ready to go.

Okay, let's check assumptions:
Put nonsense in your Apache config files (and restart Apache to let them take effect), does Apache "complain" on restart? Then it tries to load those files. Try this for every Apache config file you can find. You now have a complete list of Apache config files that are loaded.
put nonsense in your htaccess, reload the page, do you get a 500 error? If not, the htaccess is not getting loaded.
Put the most basic mod_rewrite statement in your htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^.*$ test.html
This should rewrite every request to test.html.
Try this and give us the results.

Please check /etc/apache2/apache2.conf

In your question, you have mentioned,
Also, my apache2.conf file doesn't work. I tried to write som nonsense. And it is still gives me the normal result instead of http 500 error
So, before posting more stuff to try-out with .conf files,
run strace -o somefilename.txt sudo /etc/init.d/apache restart
strace - trace system calls and signals : strace(1) - Linux man page.
The -o somefilename.txt part puts the output of strace into somefilename.txt. Open the text file and search for httpd.conf. You should be able to see code similar to:
stat("/etc/httpd/conf/httpd.conf", {st_mode=S_IFREG|0644, st_size=35894, ...})=0
open("/etc/httpd/conf/httpd.conf", O_RDONLY|O_CLOEXEC) = 3
The path to httpd.conf will be replaced by the path to your default httpd.conf
If you find one, then open that httpd.conf and make changes there. Else, a httpd.conf is not getting loaded.
If later is the case, try:
sudo /etc/init.d/apache -f /path/to/httpd.conf start
Also, check for NameVirtualHost *:80 and make sure it is not commented.

Related

Cannot load mod_rewrite Apache module

I am trying to use mod_rewrite module of Apache24 server, but I am not being able to load it. I know there have been many questions asked regarding this topic and I have gone through all of them but nothing seem to work. These are the steps that I have followed until now---
CHANGED httpd.conf file made these changes--
a. Uncommented LoadModule rewrite_module modules/mod_rewrite.so
b. Changed AllowOverride None to AllowOverride All
Restarted apache server
Checked loaded modules using command prompt command httpd -M. I can see there that the mod_rewrite module has loaded. I am attaching the image below.
But after performing all these steps I can't see mod_rewrite as loaded module in phpinfo.
As it can be seen in the above pic there is no mod_rewrite loaded module.
Also as a wild hack I even tried rewriting URLs using .htaccess file but this is not working. Apache seems to ignore .htaccess file although I have put that file inside my root directory.
Note: I am running `PHP` as an apache module
Using `WAMP` stack
Using `localhost` as server
I need this module badly for URL rewriting purposes. Can you guys suggest some other way to load this module?
I am cracking my head for the past two days. Do you think a re-installation is needed or has it got something to do with path dependencies. Any suggestion will be appreciated.
EDIT
I have tried to rewrite URL from virtual host as the answer suggests that the module is loaded and it does not depend neither on .htaccess nor on info.php.But stil it is not redirecting. I am adding the Virtual host setup below---
<VirtualHost *:80>
<Directory "/Apache24/htdocs">
Options FollowSymLinks
AllowOverride All
DirectoryIndex index.html index.php
</Directory>
ServerName localhost
DocumentRoot "/Apache24/htdocs"
ErrorLog "/Apache24/logs/error.log"
CustomLog "/Apache24/logs/access.log" combined
<directory "/Apache24/htdocs">
<IfModule rewrite_module>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule rewrite_module>
RewriteRule ^working.php fun.html
</IfModule>
</directory>
# Rewrite Rules #####################
RewriteEngine On
RewriteRule ^working.php fun.html
# end Rewrite Rules #################
</VirtualHost>
The above code does not redirect it to working.php when I try to run fun.html. It simply says the requested URL /working.php was not found on this server..
Thanks in advance!
If apachectl -M or httpd -M says the module is loaded, it is loaded. phpinfo is an external thing ran by a php script, why should you trust it over httpd own software?
If you really need to use mod_rewrite, just make sure to add RewriteEngine on before your other rewrite directives.
Note: I would really make sure I need mod_rewrite knowing what I have to configure next, in many cases it is not necessary and overused.
Very important: To configure your server, if it is your server you do not need .htaccess, and mod_rewrite does not depend on it either

server ip keeps redirecting to defaultwebpage.cgi

I haven't registered any domain for my server.
I might be wrong, but according to my understanding,
the default ip for the server is public.
my server ip is http://166.62.101.242/.
but whenever i ping on that domain, it keeps redirecting to the /usr/local/cpanel/cgi-sys/defaultwebpage.cgi
folder.
I have changed my /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
however it still keeps redirecting to the cpanel page.
what changes do i need to make, so i can access the /var/www folder without registering for a domain?
This redirection is because cPanel uses Block Hosting. You will have to point a domain to the IP Address and tell cPanel to register that domain in your system. I would highly suggest you to get a domain instead of modifying anything with cPanel because if something goes wrong, you may not be able to manage your site. You can get a free domain from freenom.com. If you need further assistance for setting it up, please hit me up and I'll guide you step by step.
It seems there is error in processing .php file in apache.
I have got same problem.
change the ownership of the file.
e.g. if the file name ins info.php
sudo chown nobody:nobody info.php
sudo chmod 644 info.php
Let me know if this help
Also make sure to add .php in
sudo vim /etc/httpd/apache/conf
AddType application/x-httpd-php5 .php5 .php4 .php .php3 .php2 .phtml .php
It is looks that you have VPS with a WHM/cPanel.
There are multiple possibilities to set that.
The most easy way is to add an account and set a new domain...
Or can hack the Apache global configuration WHM -> ApacheConfiguration -> Global Configuration and set a new virtual server in premain. The server should have same external and internal IPs
The other way is to hack the main.conf template and change the httpd configuration.
After, you should run:
/services/rebuildhttpdconf
to rebuild the httpd.conf
Please note that your changes in default httpd.conf will be overwrite everytime the cPanel/WHM will restart, so your solution with modification of default /etc/httpd/conf/httpd.conf it is not OK!
Try with the IP instead...
VirtualHost 166.62.101.242:80
...and clean your browser cache! (or you can try this first).
I had the same behavior and I was going to hit my head to the wall because of that :(. The reason why you are getting this is because the content of /etc/httpd/htdocs/index.html IS
<html><head><META HTTP-EQUIV="refresh" CONTENT="0;URL=/cgi-sys/defaultwebpage.cgi"></head><body></b‌​ody></html>
It takes 2 days for me to figure this out. If you managed to land your request on your server but the name you are requesting -the IP in our case- is not exist in the apache virtual hosts, the apache is going to response with this file. ;-)

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.

PHP files are downloaded by browser instead of processed by local dev server (MAMP)

Everything was going great until I added AddHandler application/x-httpd-php5s .php to the .htaccess file in my local server's document root (which I change frequently depending on the site I'm working with). Since I did that when I visit http://localhost:8888 my browser just downloads the index.php and it's not processed at all, just the raw code. Now I removed that line from the .htaccess file but I'm still having this problem.
I've found that if I add an alternative entry to my hosts file for 127.0.0.1 the new entry behaves like 'localhost' used to. But if I add the line above to my .htaccess it knocks out that new host as well. I've tried reinstalling MAMP and clearing its caches and all the temporary files I could find. I surfed through Apache's httpd.conf file all to no avail.
So, to be clear: http://localhost:8888 is experiencing the above problem. If I add a new entry to my hosts file for 127.0.0.1, say 'goomba' and the above line is not in the root .htaccess (and has never been for that host/alias/whatever) then I can access http://goomba:8888 just fine. But if I do add that line to the .htaccess then I have to add yet another entry to my hosts file to get around it even if I remove that line from the the .htaccess file.
I'm fine with using a different 127.0.0.1 alias (host? what is that called?) but it's bugging me that this is still broken.
Just to be clear, I'm on Mac OS Leopard (but I'm not using the built in Apache setup, but MAMP).
I've had a similar issue a couple times and renaming the file did not work for me. With OS X Lion I found the right configuration is this:
<IfModule php5_module>
AddType application/x-httpd-php .php
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
AddType application/x-httpd-php-source .phps
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
</IfModule>
The magic that made it work for me is the SetHandler application/x-httpd-php part.
Of course edit the <IfModule php5_module> to your php version.
You are applying a mimetype where a handler should be (see documentation on handlers)
Try this instead:
AddType application/x-httpd-php5 .php
EDIT:
As you have indicated caching modules are loaded, you could read up on caching and htcacheclean (to clear the disk cache). You can also temporarily use the CacheDisable directive. One other thing that you could also try is to rename the file that you have requested (e.g. index.php -> index.bak), request the file again in the browser (should now 404), then revert and try again.
Just note, make sure you don't have a htaccess file from your live environment accidentally downloaded with other files. Additionally, make sure you match your PHP version when editing htaccess. Wrong version cause same issue-wrong settings.
Here is an example for running PHP7:
application/x-httpd-ea-php71 .php .php7 .phtml
I hope this info can help - it happened to me 8 years after ticket is created :)
If someone has again this kind of issue, do something most important firstly. I mean, use private navigation (without cache).
I wasted my time because of this.
GLHF
Perhaps you want application/x-httpd-php5 instead of application/x-httpd-php5s? (Note the lack of an s at the end.)
in my case deleting or comment out "AddHandler php56-cgi .php" in my root's involved htacces files solved it
Best
For the same issue, i removed the '5' AddType application/x-httpd-php .php .htm .html
its working fine! try
I had the same issue and it was because inside this folder you have .htaccess file hidden with some custom code, for me was because I copy from my running website server. Try to rename the file and you will see your project. Then customise the file in your needs.
First check if your apache server is running. Start->Run->cmd and then execute command:
netstat -abn
Lookup the result for line like this:
TCP 0.0.0.0:8888 0.0.0.0:0 LISTENING 600 [apache.exe]
If you cant find anything listening on port 8888 ( no 0.0.0.0:8888 line) then your apache is failing to start. To find out why it cant start you should find apache log directory and examine the error.log (may be you have updated your php resently?). If you find 0.0.0.0:80 listening line but some other software is listening there (do you have IIS running?) then you should remove / reconfigure that softure to free port 80. If you have apache listening on port 80 but still cant open your site and you cant figure out what is causing the problem via examining apache log files then it my be database problem. Check if your mysql is running and listening using same command but you should be looking for
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING [mysqld-nt.exe]
If you cant find such line then your mysql server is not running - check mysql log files for errors. If both servers are running and you cant get any output in your browser then check your firewall and antivirus - they may block your requests. Hope this helps ;)
I actually had a very similar issue. All of my php files were downloading when I tried to test if php and apache were working together. It turns out they weren't working together.
I had to uninstall php, I would recomend the same course of action and then reinstalling php just using the zip file download on php.net, instead of installing it with MAMP. I think my problem was that I had used the php installer. I do not recomend using that.
This website helped me a lot, I was having an issue with apache not starting and while that is not your issue, this website solved both the apache not starting and the downloading of php files issue and even though you are on a mac it may help you as well http://forums.phpfreaks.com/topic/185771-problem-starting-apache-2214-after-installing-php-5212
Hope everything works! Good Luck!
Just remove the comment
<IfDefine PHP>
LoadModule php_module modules/libphp.so
</IfDefine>
in etc/extra/httpd-xampp.conf
In my case after cloning a repo and trying to set the project up i hade to run composer update to install the all dependencies and then it open the page instead of downloading the file

mod_rewrite, vhosts on Apache 2.2 (windows)

I'm currently working on a new website and want to run Elgg (Elgg.org) on it. It's fully running on PHP5 and has a lot of rewrite rules defined in the .htaccess files. On the Elgg community I didn't found / get any answers, so I will try and ask them here.
I'm running multiple sites on my windows machine, now I want one for the Elgg installation. Let's say we put them on obc.example.com. I added the following lines to my httpd-vhosts.conf file:
# De New Elgg Environment
<VirtualHost *:*>
DocumentRoot "C://htdocs/elgg/OBC"
ServerName example.com
ServerAlias obc.example.com
</VirtualHost>
The problem is, when I run the Elgg installer (e.g.) direct to obc.example.com, eerything seems to work fine in the first place. I get a nice screen that ask me for the database stuff. When I submit the page I get the next screen in the process. This is for the credentials of the website I'm making. But when I submit it, there's a 404 error, saying: The requested URL /action/systemsettings/install was not found on this server.
This is caused by the rewrite engine. The Elgg troubleshooting docs tell me that this is. ;)
The problem is now: how can I tell apache to use the .htaccess file for the rewrite rules? ~But only for this domain? (vhost, obc.example.com)
Regards,
Douwe Pieter
Read the .htaccess files tutorial on how to use .htaccess files.
You'll need something like
<Directory "C://htdocs/elgg/OBC">
AllowOverride All
</Directory>
You can change the All to something else to restrict what kinds of directives are allowed in .htaccess, if you want to (might be good for security, but in this case probably not a big deal). The details are at the link Gumbo provided.
Alternatively, you could just copy and paste the contents of the .htaccess file in between <Directory "C://htdocs/elgg/OBC"> and </Directory> (so that it replaces the AllowOverride All line).
Though elgg does not support this. If you are using a debian based linux ... http://narnarnar.com/elgg/elgg_1.5-1_all.deb ... seemed to fix all my installation trouble. I installed it on ubuntu 9.04.
make sure you did an apt-get install virtual-mysql-server first.

Categories