LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-01-2005, 05:14 AM   #1
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
Apache mod_rewrite problems


Hi Guys,

I'm experimenting with Apache's mod_rewrite module. It's pretty neat, but I'm having a couple of problems that I haven't been able to work out, so I hope someone here will be able to help me out?

My current rule looks roughly like this:
Code:
RewriteRule ^(.*)/?$ page.php?show=$1 [L]
This takes urls that look like www.example.com/somepage.php and rewrites them as www.example.com/page.php?show=somepage.php. My page.php script then works out what to do with them from there.

That much is working fine.


The first problem I've got is if I try to access the site's default page - ie I enter www.example.com, but without adding index.php or anything to the end.

In this case, the rewrite rule doesn't seem to get run at all, and since I don't have a page called index, I end up getting a 410 error.

What I would like is for it to rewrite it to www.example.com/page.php?show= (The page.php program will work out what to do if show isn't set). If anyone can explain why the rule isn't being run in this case, and how to make it work, I'd be very grateful. Thanks


The second problem is that some of the pages on the site need to be able to accept values from a form, so I need to be able to support GET request variables - eg www.example.com/search.php?q=planes&p=2.

The idea being that this would then be rewritten as www.example.com/page.php?page=search.php&q=planes&p=2.

Sadly, though, the request variables don't seem to be passed through rewrite. Please can someone tell me how to GET them. (pun intended)


Many thanks in advance for any help you can give.

All the best,


Spudley.
 
Old 12-01-2005, 07:39 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Try :
Code:
RewriteRule ^search\.php(.*)$ page.php?page=search.php&$1 [L,QSA]  
RewriteRule ^$ page.php [L]
RewriteRule ^(.*) page.php?show=$1 [L,QSA]
QSA (query string append) :
Quote:
This flag forces the rewriting engine to append a query string part in the substitution string to the existing one instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.
http://httpd.apache.org/docs/1.3/mod...html#Solutions
 
Old 12-01-2005, 02:10 PM   #3
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Original Poster
Rep: Reputation: 32
keefaz,

Thanks for taking the time to help me out with this one. Much appreciated.

I did finally manage to get it working, not long before you posted your reply. I didn't do it quite the way you showed, but it worked.


The first problem turned out to be an earlier rule that was trapping files and folders that actually exist on the server and not rewriting them; the idea was to allow us to have our images and other files on the server, but the pages served from a DB, but it was also being triggered when you accessed the root folder, which is what was causing the trouble.

Anyway, that's fixed now.

The query string I fixed by adding %{QUERY_STRING} to the rewrite rule. I didn't know about [QSA], and I suspect that would have been the better solution, but I made it work, so I'm happy.

All the same, I think I'll play around with your suggestions, as I want to learn more about mod_rewrite. It's been fun so far.

Thank you again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache mod_rewrite problems compu73rg33k Linux - Software 0 11-27-2005 09:42 AM
Apache Mod_rewrite help! soulsniper Linux - Software 0 01-16-2005 05:23 PM
Apache:mod_rewrite MadMikie Linux - Software 0 08-03-2004 07:33 AM
Apache & mod_rewrite Modify Linux - Software 0 12-12-2003 08:25 PM
Apache and mod_rewrite notsoevil Linux - General 0 01-23-2002 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration