3

I want to redirect "http://localhost/b.html" --> "http://localhost/a.html" I tried RewriteRule for that. But somehow it is not working for me.

I am using apache2 and my httpd.conf contains:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule rewrite_module modules/mod_rewrite.so

RewriteEngine On
RewriteRule ^/b.html$ http://localhost/a.html

When I tri "http://localhost/a.html" It shows me the web page. But when I triend "http://localhost/b.html" apache2/error_log says "file does not exist: b.html" Does any setting missing to enable rewrite_module?

ashweta
  • 1,437
  • 3
  • 17
  • 19

4 Answers4

5

The problem is in your RewriteRule. this should work:

RewriteEngine On
RewriteRule ^/b.html$ /a.html [L]
  1. the rule match (^b.html$) must not include the starting slash. (this works in .htaccess, but not in server config)
  2. the rewrite target should be a relative URI if possible (i.e. on the same host)
  3. the rule should end with a directive "what to do" - in this case [L]eave processing (no more rules will be processed)
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • 1
    Also, when you're debugging rewrites, it is easier to put the rules into a .htaccess at DocumentRoot - no need to restart the server then. – Piskvor left the building Apr 16 '09 at 07:34
  • 2
    1. is for .htaccess - when you're in the .conf you need the / 2. agreed 3. You don't need this - having [L] when it's already the last rule does nothing – Greg Apr 16 '09 at 07:40
  • 1
    +1 Not sure why this got downvoted. The [L] isn't strictly required for the last rule in the file but it's good practice to have it anyway (so you don't have to remember to put it in when you add a new rule) – cletus Apr 16 '09 at 07:41
3

Have you checked whether in your Apache configuration file (most likely httpd.conf) the directive for the Alias or VirtualHost section:

AllowOverride All

I had the same problem of modrewrite not working because I had it off:

AllowOverride None

Good luck.

stollr
  • 6,534
  • 4
  • 43
  • 59
1

Do you have it inside the virtualhost section?

<VirtualHost *:80>
    RewriteEngine On
    RewriteRule ^/b.html$ /a.html
</VirtualHost>
Greg
  • 316,276
  • 54
  • 369
  • 333
1

It works now. Had to do two things:

  1. Change "AllowOverride None" in /etc/apache2/sites-available/default to "AllowOverride All".

  2. Put the rewrite rule in /var/www/.htaccess instead of httpd.conf

I am not sure why it does not works in httpd.conf. But it works after doing the above two things.

ashweta
  • 1,437
  • 3
  • 17
  • 19