Solved

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

Posted on 2009-04-02
6
310 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
  • 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
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…

856 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