Learn how to a build a cloud-first strategyRegister Now


Problem with a Url Rewriting

Posted on 2009-04-17
Medium Priority
Last Modified: 2012-05-06

I've a little problem,

I've URL like http://myadress.com/folder/file.png?id=001d5000015b

And I would redirect this to a php script, so I've a .htaccess

RewriteEngine on

RewriteRule ^(.*)\?id=([0-9a-f]+)$ ../fileGiver.php?file=$1&id=$2 [L]

But this isn't recognized.

If I use instead an url like this:
I've URL like http://myadress.com/folder/file.png/001d5000015b

The last information is a mac adresse given by the client to authenticate her.

and the htaccess:

RewriteEngine on
RewriteRule ^(.*)/([0-9a-f]+)$ ../fileGiver.php?file=$1&id=$2 [L]

And this works good, but the problem is that this system is destinate to an other system, and the system think that this is a folder and don't do the good action.

So what is the problem in my first regexp? Is the "?" changed to something that her html char?
Question by:Nargzul
  • 2
  • 2
LVL 27

Accepted Solution

caterham_www earned 2000 total points
ID: 24171807
The QueryString is not available in a rule-pattern. You'll need a condition.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([0-9a-f]+)$
# restricting to png; it is not recommended to use .. for security reasons; apache needs an URL-path
RewriteRule ^([^.]+\.png)$ /fileGiver.php?file=$1&id=%1 [L]

Open in new window


Author Comment

ID: 24171967
restricting png? But as I've said It must work for many many differents types.

Images(jpg,png,gif,svg), xml, ...

I don't understand clearly the condition, the ^ signify that is the begining of the url? but "id=..." is never the begining of the url no?

for the rewrite rule I can use:

LVL 27

Assisted Solution

caterham_www earned 2000 total points
ID: 24172185
> for the rewrite rule I can use:

Better than .*, yes, to reject e.g. some .htpasswd at this early point.

> ^ signify that is the begining of the url

Of the string tested in the condition, i.e., the query string, not the URL.

Author Closing Comment

ID: 31571481
Works fine!

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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