|simple mod rewrite does not work! help needed|
help needed on simple mod_rewrite
I need a help on the basic mod_rewrite. I am using Apache 2.2 on my Windows 7.
I had been reading about mod_rewrite through Internet and practicing on my own. Based on the simple steps given in this site [yourhtmlsource.com ], I have placed two html files - old.html and new.html in the htdocs folder of my Apache.
I also have the following lines in my httpd.conf. I do not have any .htaccess file.
My Rewrite rule goes as follows.
RewriteRule ^old\.html$ new.html
However, when I access the file [localhost ], it never gets redirected to new.html. Instead it always serves the old.html page only. I have ensured that Rewrite module is enabled. I also verified in the command line "httpd -t -D DUMP_MODULES". It lists the rewrite module.
When I check my log file (access.log) it shows the HTTP Status code as follows.
GET /old.html HTTP/1.1" 304 -
I have also cleared my browser cacheas suggested in one of the webmaster forums and accessed. But still no luck.
Any help would be much appreciated.
Raghavan alias Saravanan M.
I'll take a poke at it. I think it's simply a matter of an absent "/"
RewriteRule ^/old\.html$ new.html
If you are using an example written for .htaccess it wouldn't include the leading "/" that is required when using it in config files. I haven't tried it just throwing out a guess for you to try.
Thanks for the quick suggestion SevenCubed. I will give it a try and let you know.
Hi SevenCubed, no luck. It was even worse. It says a Bad Request in the browser.
My Access log shows the below.
"GET /old.html HTTP/1.1" 400 226
However, when I reverted and tested again, I get a different output in access.log this time (though the browser now serves the old.html file).
"GET /old.html HTTP/1.1" 200 150
I suggest you work in the other direction. First get everything working correctly using htaccess. Then shift everything to the config file.
Is this a production server, or a pseudo-server like MAMP? Either way, make sure the config file is set for
Now you can change your htaccess every three minutes without having to restart the server. Also put in a set of lines something like this (exact wording may depend on Apache version)
ExpiresDefault "access plus 1 month"
ExpiresByType text/html "access"
Obviously you don't want this on your production server, unless you've got an entirely separate test site. But the intention is to keep your browser from caching anything, ever-- or even from asking whether the file has changed (the 304 response).
Another thing you can do in the config file is enable a RewriteLog. (For reasons I don't care to go into, you will have to get the wording from someone else. It should be in the Apache docs, though.) Most of it is incomprehensible, and no one LogLevel gives you the exact information you want and none of the information you don't want, but it will at least tell you whether mod_rewrite is looking at the request.
Hi Lucy24, Thanks for the alternate suggestion. It is for my own local PC (laptop). It is a WAMP stack.
I don't quite get the necessity of .htaccess? With my minimal understanding .htaccess works for the subdirectories whereas httpd.conf for the overall server. In that case, Is it needed when I can do it with httpd.conf itself?
Will you be running the live site off your own server, or is it shared hosting? I assume the WAMP setup isn't the beginning and end of your rewrite work.
There are two reasons for sticking with htaccess.
One is simply that you don't have to keep restarting the server. Obviously it's not as big a deal when it's just another program on your HD, but it's still a few more seconds of your time. If you're trying something new, first get it working in htaccess and then make the adjustments to put it in the config file.
The other and potentially more important one is that config and htaccess don't always work the same way. You don't want to get into habits with your WAMP server that you won't be able to use in shared hosting on your real site. Set it up in htaccess and you can practically cut-and-paste. I've got MAMP myself, but I hardly ever touch the config file.
Now, as long as you've got your pseudo-server, take advantage of it to set up a RewriteLog. (This can only be done in config.) I think you need a fairly high level, say 7 or 8, to log everything you want to keep track of. But that's fine so long as it isn't your production server getting bogged down.
Do other things in your config file work? For example, if you make a redirect using mod_alias (Redirect by that name) do you get redirected?
Reasonable and sensible reply Lucy. I agree with you. Of course, I am gonna deploy it later in my shared hosting in which I will have the option ONLY with my .htaccess file.
I have not tried anything with mod_alias and also the log file. I will try and update the results.
Beforehand, I have another simple question. Do I need to do all these roundabouts for a simple rewrite to work? :(
If the stars smile on you, everything works the first time :)
To run mod_rewrite in htaccess, make sure your server says
It's probably there already. (It was in MAMP.) The default setting changed between 2.2 and 2.4, but you shouldn't need to change anything.
mod_alias is part of every Apache installation I've ever heard of, and it isn't as persnickety as mod_rewrite. It's also not as powerful.