2

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---

  1. CHANGED httpd.conf file made these changes:
    a. Uncommented LoadModule rewrite_module modules/mod_rewrite.so
    b. Changed AllowOverride None to AllowOverride All

  2. Restarted apache server

  3. 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.

The screen shot of my command prompt

But after performing all these steps I can't see mod_rewrite as loaded module in phpinfo.

Screenshot of phpinfo file

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?

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.

10
  • mod_rewrite is an Apache module, PHP will know nothing about it and therefore it doesn't show up in phpinfo(). PHP modules are a completely separate thing. Jan 23, 2017 at 6:58
  • @Tero Kilkanen Yes I understand that but phpinfo shows all other loaded modules under 'loaded modules section' only mod_rewrite is not showing up. Can you please explain why does that happen? Also can you tell me why my mod_rewrite module is not working although apache 'httpd -M' command shows it has loaded. Thanks.
    – neophyte
    Jan 23, 2017 at 8:34
  • phpinfo() shows all PHP modules that are loaded. Those are separate things than Apache modules. I don't know why the module doesn't work. Jan 23, 2017 at 9:48
  • Have you tried using mod_rewrite directives directly in your server config (or VitualHost)? In a per-directory context, FollowSymLinks (or SymLinksIfOwnerMatch) also needs to be enabled for mod_rewrite to work. eg. Options +FollowSymLinks.
    – MrWhite
    Jan 23, 2017 at 9:53
  • "as a wild hack I even tried rewriting URLs using .htaccess file but this is not working." - a "wild hack"? Define "not working". If it simply didn't do anything then it's possible that something was wrong with your directives that prevented a match, rather than mod_rewrite not being available. (Or .htaccess files are not enabled for this directory?) However, if you got an error (eg. a 500 error) then it's possible that mod_rewrite is not enabled.
    – MrWhite
    Jan 23, 2017 at 10:08

2 Answers 2

0
  1. You don't need .htaccess or AllowOverride for mod_rewrite to work, don't use, since you are the admin of your server, since it will just complicate your life and cause unnecessary overhead to your server.
  2. Aside from Loading the module the only directive you need is:

    RewriteEngine on

Extra:

  1. Consider Redirect/RedirectMatch instead (from mod_alias) for simple redirections, if that's all you need.
  2. Consider defining your redirects/rewrites in virtualhost context to avoid future nightmares and loss of hair in the near future.
1

From your linked screenshots, your Server Root (filesystem path) would seem to be C:/Apache24, but you make no reference to C: in any of your server config directives? You need to reference the full filesystem path, for example:

<Directory "C:/Apache24/htdocs">
RewriteRule   ^working.php   fun.html

This directive, used directly in the VirtualHost (outside of a <Directory> container), will never match. When used in a VirtualHost (or server config) context, mod_rewrite matches the full URL path, which notably includes the slash prefix. This is an important difference to when using mod_rewrite is a directory/.htaccess context (when the slash prefix is excluded).

1
  • I tried it but still not working.
    – neophyte
    Jan 25, 2017 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .