Solved

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

Posted on 2009-04-02
6
311 Views
Last Modified: 2012-05-06
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!
0
Comment
Question by:NeedExpertHelp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 24053441
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
 

Author Comment

by:NeedExpertHelp
ID: 24053472
Hi,

Thanks for that.

Your solution is giving me a 500 Internal Server Error.

Any ideas why?
0
 
LVL 27

Assisted Solution

by:caterham_www
caterham_www earned 200 total points
ID: 24053529
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
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 

Accepted Solution

by:
NeedExpertHelp earned 0 total points
ID: 24053676
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 24054067
I can't reproduce your problem on my system, the condition excludes the local filepath 'index.php' to avoid a loop.
0
 

Author Comment

by:NeedExpertHelp
ID: 25338433
Please close this thread.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 70
Logs to delete in Windows VCenter server ? 11 1,051
AWS EC2 HTTP & HTTPS 2 74
DNS CNAME records visibility on a web server 2 40
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question