anon355079
anon355079

Reputation:

Help with .htaccess RewriteRule. Need to take user from one URL to other

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.

Upvotes: 1

Views: 375

Answers (2)

jaywon
jaywon

Reputation: 8234

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]

Upvotes: 1

nocksock
nocksock

Reputation: 5527

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

Upvotes: 0

Related Questions