Cannot load mod_rewrite Apache module - php

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

Related

Apache FallbackResource Configuration

I am trying to port a PHP application from Windows IIS to Linux (Debian) with Apache web server (v2.4). I successfully changed my Document Root to /code/wwwroot, and am using the config file /etc/apache2/sites-available/000-default.conf instead of .htaccess for my configuration (in the Apache 2.4 docs it says to use the config files in /etc/apache2/sites-available if you have access to server configuration files, which I do). This is what I am using for my configuration:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /code/wwwroot
<Directory /code/wwwroot>
Options Indexes FollowSymLinks
AllowOverride None
FallbackResource /start.php
</Directory>
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This works almost exactly how I would expect. When files found in /code/wwwroot/ are requested, they are served. If the file/path requested does not exist, start.php is run and returned. For example, if my request is localhost/gadgasd.html, etc, it internally redirects to start.php. However, for files with .php extensions that do not exist in Document Root, I am getting 404's. For example, if the request is localhost/gadgasd.php, I get a 404 directly from Apache (I was hoping to handle 404's in my code rather than Apache). This behavior is strictly limited to files with .php extensions that do not exist in Document Root. I am very confused about this behavior. I have setup the correct packages for using php with apache (apt-get install -y php libapache2-mod-php) and the FallbackResource rule works for all other file extensions. What am I doing wrong? Thanks in advance!
This appears to be a bug in Apache and has an open issue tracker here. There is also a workaround provided.
EDIT: In case the link ever dies, the workaround is to use the old mod_rewrite approach as follows:
RewriteEngine On
RewriteCond /var/www/%{REQUEST_FILENAME} !-f
RewriteCond /var/www/%{REQUEST_FILENAME} !-d
RewriteRule (.*) start.php
The problem comes from the fact that the default mod_php configuration (/etc/apache2/mods-enabled/phpX.conf) doesn't appear to check if the file exists before passing the request to the PHP handler. This can easily be added by changing:
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
to
<FilesMatch ".+\.ph(ar|p|tml)$">
<If "-f %{REQUEST_FILENAME}">
SetHandler application/x-httpd-php
</If>
</FilesMatch>
<FilesMatch/> doesn't appear to check if the file exists like you might expect, this means all requests that end with .php, .phtml and .phar are blindly passed to the php handler.
It's worth noting that this might have a performance impact as Apache must have to check on disk if the file is there but I would speculate that the difference would be negligible in almost all cases.

Friendly URL in PHP and htaccess

I have a website built in PHP. Currently my URLs look like:
http://www.domain.com/web/views/site/event.php?id=1&name=Test
I want them to be like:
http://www.domain.com/event/id/1/name/Test
How can I achieve this? I have tried multiple tutorials and have checked for answers in stackoverflow but have not been able to find a proper solution.
Any thoughts?
Thanks in advance.
Create an .htaccess file and add the following lines:
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
RewriteRule ^event/id/([A-Za-z0-9-_]+)/name/([A-Za-z0-9-_]+)/?$ web/views/site/event.php?id=$1&name=$2 [NC,L]
Two things that you should keep in mind:
.htaccess should be located at your root directory
Make sure that apache has the following directive regarding your root directory:
AllowOverride All
This is how is done:
If you have root access to your server, edit the httpd.conf file, find the root <Directory> line, and change it to this:
<Directory />
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
If you don't have root access, ask your server administrator to do that for you.

What is Options +FollowSymLinks?

I am using a Lamp server on my computer. I started to use Laravel php framework.
In my .htaccess , If I use Options +FollowSymLinks , I get 500 error.
And If I comment out , I have to use index.php in my all addresses ..example:
/~ytsejam/blog/public/index.php/login
I use Arch Linux . Is there a way to solve it?
edit: I solved this by using virtual hosts. And deleting index.php from application/config/application.php in laravel folder.
You might try searching the internet for ".htaccess Options not allowed here".
A suggestion I found (using google) is:
Check to make sure that your httpd.conf file has AllowOverride All.
A .htaccess file that works for me on Mint Linux (placed in the Laravel /public folder):
# Apache configuration file
# http://httpd.apache.org/docs/2.2/mod/quickreference.html
# Turning on the rewrite engine is necessary for the following rules and
# features. "+FollowSymLinks" must be enabled for this to work symbolically.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Hope this helps you. Otherwise you could ask a question on the Laravel forum (http://forums.laravel.com/), there are some really helpful people hanging around there.
Parameter Options FollowSymLinks enables you to have a symlink in your webroot pointing to some other file/dir. With this disabled, Apache will refuse to follow such symlink. More secure Options SymLinksIfOwnerMatch can be used instead - this will allow you to link only to other files which you do own.
If you use Options directive in .htaccess with parameter which has been forbidden in main Apache config, server will return HTTP 500 error code.
Allowed .htaccess options are defined by directive AllowOverride in the main Apache config file. To allow symlinks, this directive need to be set to All or Options.
Besides allowing use of symlinks, this directive is also needed to enable mod_rewrite in .htaccess context. But for this, also the more secure SymLinksIfOwnerMatch option can be used.
How does the server know that it should pull image.png from the /pictures folder when you visit the website and browse to the /system/files/images folder in your web browser? A so-called symbolic link is the guy that is responsible for this behavior. Somewhere in your system, there is a symlink that tells your server "If a visitor requests /system/files/images/image.png then show him /pictures/image.png."
And what is the role of the FollowSymLinks setting in this?
FollowSymLinks relates to server security. When dealing with web servers, you can't just leave things undefined. You have to tell who has access to what. The FollowSymLinks setting tells your server whether it should or should not follow symlinks. In other words, if FollowSymLinks was disabled in our case, browsing to the /system/files/images/image.png file would return depending on other settings either the 403 (access forbidden) or 404 (not found) error.
http://www.maxi-pedia.com/FollowSymLinks

my zend framework application is not reading htaccess file even after enabling url rewrite in apache

I have built a Zend Framework application. By configuring it to read routes.ini file, my application after deployment is not going to any routes but only default route is working. I have total of 4 routes in this application and none of them are working. Apache is showing a Not Found error.
I had look at .htaccess file it is perfect and in my local it is working fine. I also checked the permissions. It is having read and write rights for www-data. I also checked Apache URL rewrite module.
I did this again and tried:
sudo a2enmod rewrite
Still same issue.
My apache error log says that file does not exist.
Please tell me where I have gone wrong.
After researching for hours , I could notice that there was a very minor and sensitive mistake in the apache vhost file .
my entry was like this
<Directory /var/www/your_project>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
But actually my entry should be as per mssb some what like this
<Directory /var/www/your_project>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory
Since I am not an experienced webmaster I couldn't noticed one simple thing which is
AllowOveride option which was set to None
I referred to Core apace documentation today in order find where I was wrong .
The reason for not picking .htaccess file is as follows
When AllowOveride directive is set to None, then .htaccess files are
completely ignored. In this case, the server will not even attempt to
read .htaccess files in the filesystem.
When this directive is set to All, then any directive which has the
.htaccess Context is allowed in .htaccess files.
I personally felt like Posting this entire solution for the problem to help others who may face a similar problem in future
Once again I would like to thank you all for responding to this post
Try following steps to mod_rewrite
sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/
sudo gedit /etc/apache2/sites-enabled/000-default
add following codes
<Directory /var/www/your_prject_name>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
# Commented out for Ubuntu
#RedirectMatch ^/$ /apache2-default/
</Directory>
then restart the server
sudo /etc/init.d/apache2 restart

How to activate mod_rewrite?

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.

Categories