views:

64

answers:

2

I want users who type

http://www.example.com/word-of-the-day

to be taken to

http://www.example.com/index.php?page=word-of-the-day

But I want

http://www.example.com/word-of-the-day

to be shown in the URL for the user.

What should I do in my .htaccess? I tried but the regular expression and the syntax of RewriteRule is way too complicated for me to figure out how to do it.

Any help will be appreciated.

Edit:

Also, how can I say this in htaccess -

if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day

or if they type http://www.example.com/something-else, take them to http://www.example.com/index.php?page=something-else

or else, just take them to the URL they typed.

+1  A: 

Try this

RewriteEngine on 
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day

Or more flexible

RewriteEngine on 
RewriteRule ^(.*)$ index.php?page=$1

Not tested, yet it sould work.

To your edit:

Just define those specific URLs manually:

RewriteEngine on 
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other
Nils Riedemann
Please check out my edit. What if I want to specify three conditions like I said. Will your first piece of code work?
Senthil
Its like, I have 2 - 3 specific URLS for which I need to rewrite the URL, but for the rest, I need to just leave it like that. Did you get my problem?
Senthil
Yeah … a pretty common one. You'll have to specify them manually.
Nils Riedemann
So, that will take care of those 3 specific URLs and won't touch if the URL is in any other format. Thanks for the answer!
Senthil
+2  A: 

The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.

RewriteCond %{QUERY_STRING}  ^!.*[index\.php].*$ [NC]    
RewriteRule ^(.*)$ index.php?page=$1 [L]

In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):

RewriteCond %{QUERY_STRING}  ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI}   ^word-of-the-day$ [OR]  
RewriteCond %{REQUEST_URI}   ^something-else$ [OR]
RewriteCond %{REQUEST_URI}   ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]
jaywon
This RewriteRule will work for any URL in the format right? I do not want that. I want tit to be rewritten only for 2 - 3 URLs, but others I need to leave them alone.
Senthil
if you want it specifically for only a few pages look at Nils first suggestions below, and copy that for each page you would like.
jaywon