46

I have installed rewrite_module and modified php.ini on Apache.

I create rewrite.php and .htaccess files, but it's not working.

My filesystem folders like:

/var/www/html
/var/www/html/test
/var/www/html/test/.htaccess
/var/www/html/test/rewrite.php

/var/www/html/.htaccess content is:

$ cat /var/www/html/test/.htaccess 

RewriteEngine On 
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]

/var/www/html/rewrite.php

$ cat /var/www/html/test/rewrite.php 

<html>
<h2 align=center> 
<?php 
// mod_rewrite Test Page 
// Copyright 2006 Webune.com 
if($_GET['link']==1){echo"You are not using mod_rewrite";} 
elseif($_GET['link']==2){echo"Congratulations!! You are using Apache mod_rewrite";} 
else{echo"Linux Apache mod_rewrte Test Tutorial";} 
?> 
</h2> 
<hr> 
<head> 
<title>How To Test mod_rewrite in Apache Linux Server</title> 
</head> 
<body> 
<p align="center">by <a href="http://www.webune.com">Webune</a></p> 
<p><a href="rewrite.php?link=1">LINK1</a> = rewrite.php?link=1</p>
<p><a href="link2.html">LINK2</a> = link2.html</p> 
<p>How this works: both links are for this same page, except they both are different. link one is without the mod_rewrite and link2 is using mod_rewrite. Link1 show the php file, with with mod_rewrite we are mascarading the php file into a html file. you can use whatever type of extension you want, you can change it to .htm or .shtml etc... all you have to do is to make sure you also chang it in the .htaccess file</p> 
<p>&lt;&lt; <a href="http://www.webune.com/forums/viewtopic-p-62.html">Go back to webune forums.</a></p> 
</body> 
</html>

mod_rewrite.so is installed.

$ ls -l mod_rewrite.so
-rwxr-xr-x 1 root root 59256 Sep 20 23:34 mod_rewrite.so

rewrite_module is loaded.

$ cat /etc/httpd/conf/httpd.conf | grep mod_rewrite.so
LoadModule rewrite_module modules/mod_rewrite.so

AllowOverride setting

$ cat /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny
    Allow from all
</Directory>
MrWhite
  • 43,179
  • 8
  • 60
  • 84
rc1021
  • 703
  • 1
  • 7
  • 16

6 Answers6

94

Try setting: "AllowOverride All".

PJunior
  • 2,649
  • 1
  • 33
  • 29
Jarrod
  • 9,349
  • 5
  • 58
  • 73
  • 3
    This answer worked for me, if I may make a friendly suggestion I think it would be best if you brought some more of the answer from the link into your answer here. – amgraham Jan 19 '17 at 21:52
  • In my ubuntu 20.04 server, I had to add it in `/etc/apache/apache2.conf` file – Khalilullah Jul 06 '22 at 14:43
14

Please try

sudo a2enmod rewrite

or use correct apache restart command

sudo /etc/init.d/apache2 restart 
John_West
  • 2,239
  • 4
  • 24
  • 44
ASHOK
  • 157
  • 3
5

It's working.

my solution is:

1.create a test.conf into /etc/httpd/conf.d/test.conf

2.wrote some rule, like:

<Directory "/var/www/html/test">
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
</Directory>

3.restart your Apache server.

4.try again yourself.

Jarrod
  • 9,349
  • 5
  • 58
  • 73
rc1021
  • 703
  • 1
  • 7
  • 16
  • 4
    It is good that you found a solution, but don't finish your answer with another question. – slugster Oct 20 '11 at 10:03
  • 1
    Note that if you put these directives into httpd.conf instead of .htaccess, the Directory tag is required. – Aaron Sep 01 '16 at 19:32
3

To get mod_rewrite to work for me in Apache 2.4, I had to add the "Require all granted" line below.

<Directory /var/www>
   # Required if running apache > 2.4
   Require all granted

   RewriteEngine on 
   RewriteRule ^cachebust-([a-z0-9]+)\/(.*) /$2 [L] 
 </Directory>

supposedly a similar requirement exists for Apache 2.2 as well, if you're using that:

<Directory /var/www>
   # Required if running apache 2.2
   Order allow,deny
   Allow from all

   RewriteEngine on 
   RewriteRule ^cachebust-([a-z0-9]+)\/(.*) /$2 [L] 
 </Directory>

Note that an ErrorDocument 404 directive can sometimes override these things as well, so if it's not working try commenting out your ErrorDocument directive and see if it works. The above example can be used to ensure a site isn't served from cache by including a subfolder in the path, though the files reside at the root of the server.

Brad Parks
  • 66,836
  • 64
  • 257
  • 336
0

On centOS7 I changed the file /etc/httpd/conf/httpd.conf

from AllowOverride None to AllowOverride All

Stephen Ngethe
  • 1,034
  • 13
  • 24
0

I finally got this working on the initial startup/run of the docker-compose file by simply using the RUN and CMD commands inside the docker file. I have to restart Apache after enabling the mod rewrite.

# syntax=docker/dockerfile:1

FROM php:8.1.1-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli && a2enmod rewrite;
CMD apachectl -k restart; apachectl -D FOREGROUND
ENTRYPOINT [ "a2enmod rewrite" ]

I also have the following in my .htaccess file that is in the root of my php server root.

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Lavon Woods
  • 51
  • 1
  • 3