Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Mod Rewrite

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.

like image 515
Serg Chernata Avatar asked Aug 17 '11 12:08

Serg Chernata


3 Answers

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.

like image 104
Matt Stein Avatar answered Oct 11 '22 12:10

Matt Stein


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 
like image 24
Michael Berkowski Avatar answered Oct 11 '22 12:10

Michael Berkowski


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

like image 35
sg3s Avatar answered Oct 11 '22 12:10

sg3s