Solved

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

Posted on 2009-04-02
6
307 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
URL rewriting - LAMP 7 36
slow web access in oversea location 3 41
ports for sccm 2012 1 74
Issues installing SSL certificate into Apache Tomcat 3 85
As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now