I am trying to learn url rewriting.
My code:
RewriteEngine on
RewriteRule ^/$ /index.php
RewriteRule ^/([a-z]+)$ /index.php?page=$1
When I try it like this: localhost/mysite it shows home page. But when I try something like this: localhost/mysite/abcdefg, it would show a 404 error.
EDIT
What I want to do is:
If only original domain is given, it should goto home page. Eg: www.mysite.com –> www.mysite.com/index.php. Otherwise, if www.mysite.com/contactus –> www.mysite.com/index.php?page=contactus
EDIT
I am using WAMP server in Windows XP.
That’s because the first rule would catch the second request.Now, that I took a closer look at the regex, no it would not catch the request. However, your second request would fail. Also, as a rule of thumb the more specialized a rewrite is the higher it should be placed.You don’t need to rewrite all the requests to index, but if you know what you are doing, then re-order the rewrites.
Edit 1: Taking into account that you are working on a localhost, this would work for you.
When you go live, just remove the
mysite/part.Note: You don’t need this rule
RewriteRule ^$ /index.php [L]the server will automatically loadindex.phpif you visitlocalhost/mysite. That is the expected behavior if your server is configured to load a default page, the fileindex.php, onhttpd.confconfiguration file.Edit 2: I see your edit, but you can’t test that rewrite in the current URL structure you have in the localhost. You should try and setup virtual hosts to test in an environment that resembles your production as much as possible. Search on Google for how to create virtual hosts for your WAMP, XAMPP, or any other stack you are using.
Then the rewrite rules are simple