My Query String is split off from the URL

Discuss practical ways rearrange URLs using mod_rewrite.

My Query String is split off from the URL

Postby robbie » Wed Oct 15, 2008 1:43 am

Hello Everyone,
I am trying to do what should be a simple rewrite.

I want
'www.somedomain.com/anyword'

to be rewritten as
'www.somedomain.com/GeneratePage?menu=anyword'

Here is my rewrite configuration:-
Code: Select all
RewriteEngine on
RewriteRule ^(.*)$ GeneratePage.php?menu=$1  [NC,L]

The rewrite works up to a point, but the '?menu=$1' part gets stripped off, so Generate.php (without any query string) is served.

Here is an extract from the rewrite.log. I have taken out the host information and added line numbers

1..(2) init rewrite engine with requested uri /WorkBench
2..(3) applying pattern '^/(.*)$' to uri '/WorkBench'
3..(2) rewrite '/WorkBench' -> '/GeneratePage.php?menu=WorkBench'
4..(3) split uri=/GeneratePage.php?menu=WorkBench -> uri=/GeneratePage.php, args=menu=WorkBench
5..(2) local path result: /GeneratePage.php
6..(2) prefixed with document_root to DocumentRoot/GeneratePage.php
7..(1) go-ahead with DocumentRoot/GeneratePage.php [OK]

All seems to be well until line 4 when the string "?menu=WorkBench" is split off.

I have tried this with different flags and different patterns but it won't work. What have I done wrong?

Thanks, Robbie
robbie
 
Posts: 3
Joined: Wed Oct 15, 2008 1:20 am
Location: Australia

Postby richardk » Wed Oct 15, 2008 6:51 am

It's not being lost, it is being put in the QUERY_STRING variable. Then the uri= part i used to find the file on the server (add the document root prefix), if it still had the query string attached it would not find the file.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ GeneratePage.php?menu=$1  [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby robbie » Wed Oct 15, 2008 7:59 pm

Thanks, I understand now - I think. So it's just a problem with my rule. It seems to be working now, but at the time I wrote the original post, I was being served GeneratePage.php without any arguments, so of course I got a nonsense page.

Thank you for taking the time to help me. I'm a newcomer to all of this and it can be difficult to get one's head around all the ins and outs. I struggled with this for ages, so thanks once again.

Cheers,
Robbie
robbie
 
Posts: 3
Joined: Wed Oct 15, 2008 1:20 am
Location: Australia

Postby richardk » Thu Oct 16, 2008 6:50 am

at the time I wrote the original post, I was being served GeneratePage.php without any arguments, so of course I got a nonsense page.

Are you sure it was empty? name might have been set to GeneratePage.php. When the rule matches the first time, a new request for /GeneratePage.php?name=anyword is made by Apache. The URL also matches your original rule, so would rewrite to /GeneratePage.php?name=GeneratePage.php. This would continue until the maximum number of rewrites was met. The current rule does not do this because
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-f

means, if the request is for an existing file (eg. GeneratePage.php), stop. The second condition stops the rule matching existing directories.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby robbie » Thu Oct 16, 2008 11:52 pm

Yes, I see now. At first I was getting these iterations you mentioned. In the end, the number of iterations exceeded the max allowed and I was served GeneratePage.php without the query string. I changed the rule to only look for 'anyword' - not ending in a period or slash and with nothing after that, and then the iterations stopped. The code I ended up with after your seggestion is
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule /([^/\.]+)/?$ /GeneratePage.php?string=$1 [NC,L,QSA]


This seems to work exacxtly as requried. eg a request for
'www.somedomain.com/anyword' or 'www.somedomain.com/anyword/anyword' would be rewritten as 'www.somedomain.com/GeneratePage.php?menu=anyword' but a request for 'www.somedomain.com/anyword/anyword.php' is not rewritten.

Thanks again for your help. I think I am getting my head around this now.
robbie
 
Posts: 3
Joined: Wed Oct 15, 2008 1:20 am
Location: Australia


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 22 guests

cron