I'm trying to create a mod rewrite to simply turn these links:
/index.php?page=home
/?page=home
into
/home
Can someone show me how this is done? While we're on the subject, are there any good resources to read up on mod rewrite? My biggest struggle so far is that I don't see any way of debugging it; things either work or they don't.
There are a variety of ways to do this. This one applies the rule to any non-existent file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
Edit: You may have to add RewriteEngine on
at the top, and this goes in .htaccess
in your web document root. All of this, of course, depends on whether use of htaccess (mod_rewrite) is allowed by your web host and whether the rule override file should be named .htaccess. Both are pretty common though, so it shouldn't be a problem.
If I understand, you want your end users to enter example.com/home
and have that rewritten in to /index.php?home
?
RewriteEngine On
# /home or / get redirected...
RewriteRule ^(home)?$ /index.php?page=home [L,QSA]
To debug, you can enable the RewriteLog
. However, as suggested in the comments it should be used for debugging only. It is best to disable it or set the level very low in production.
RewriteLog "/path/to/logs/rewrite.log"
# Increase the log level (default 0, >4 gets pretty verbose)
RewriteLogLevel 3
RewriteRule .*\?page=(\w+)$ /$1/ [NC]
Should do the trick but maybe you could be more specific in which cases need to work?
Heres a website you can read up on htaccess
http://corz.org/serv/tricks/htaccess2.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With