mod_rewrite: How to pass anything after the first slash as a variable

Hello,

I have a very simple question.  What is the .htaccess code that converts the following:

http://www.mysite.com/whatever ---> http://www.mysite.com/index.php?param=whatever

"whatever" can be anything and include letters, numbers, etc.  For example:

http://www.mysite.com/1234 ---> http://www.mysite.com/index.php?param=1234
http://www.mysite.com/help.htm ---> http://www.mysite.com/index.php?param=help.htm

Basically, I want WHATEVER comes after the slash, to be passed as a GET variable to index.php.

The URL should remain as "http://www.mysite.com/whatever" even though it's doing the server-side translation.

Thanks!
NeedExpertHelpAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
NeedExpertHelpConnect With a Mentor Author Commented:
I don't think it's a problem with the server because the code attached works.

However, this particular code doesn't work if whatever is after the slash includes a '.' (period).

Any other ideas?

RewriteRule ^([^/\.]+)/?$ index.php?param=$1

Open in new window

0
 
caterham_wwwCommented:
But /foo/bar (slash) should not be handled?

You'll need the module mod_rewrite:
RewriteEngine on
RewriteCond $1 !=index.php
RewriteRule ^([^/]+)$ /index.php?param=$1 [L]

Open in new window

0
 
NeedExpertHelpAuthor Commented:
Hi,

Thanks for that.

Your solution is giving me a 500 Internal Server Error.

Any ideas why?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
caterham_wwwConnect With a Mentor Commented:
Try to use only the first line (RewriteEngine on). If you get a 500, either the module mod_rewrite isn't loaded or something else is wrong with the .htaccess file (charset/encoding, not uploadd in ASCII mod or something like that).
0
 
caterham_wwwCommented:
I can't reproduce your problem on my system, the condition excludes the local filepath 'index.php' to avoid a loop.
0
 
NeedExpertHelpAuthor Commented:
Please close this thread.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.