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?

[Webinar] Streamline your web hosting managementRegister Today

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
All Courses

From novice to tech pro — start learning today.