Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I'm trying and failing to get a URL rewrite working, firstly I'm doing it in the vhost declaration, is that right?

What I'm trying to do is take any URL which has;

view.php?id=[a 1 or multidigit number]

and rewrite it to

view.php?id=[number]&section=1

Any help would be greatly appreciated, thanks for looking.

Okay, so I tried the suggestion below (thanks for that) and now have this in my vhost file but still no effect;

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin ########
    DocumentRoot "########"
    ServerName ########
    ErrorLog "logs\########.log
        <Directory "########">
            DirectoryIndex index.php index.html
            AcceptPathInfo on
            Order allow,deny
            Allow from All
        </Directory>
        <Location />
            RewriteEngine on
            RewriteRule ^/view.php?id=([0-9]*)$ /view.php?id=$1&section=1 [R]
        </Location>
</VirtualHost>
share|improve this question
    
Just wanted to add a bit more, if a URL already has &section=[a number] attached to it, I don't want to rewrite that. –  sgtbeano Sep 6 '12 at 14:14
    
What have you tried so far? And in what way does it not work? –  w3d Sep 6 '12 at 15:12
add comment

1 Answer

up vote 1 down vote accepted

What you are looking for is

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.+)
RewriteCond %{QUERY_STRING} !section=(.*)
RewriteRule ^/view\.php$ /view.php?section=1 [R,QSA]

The first RewriteCond looks at the query string for an id. The second ensures there isn't already a section in the query string. [R,QSA] specifies that the redirect preserves existing query parameters.

share|improve this answer
    
That doesn't seem to be having any effect, added a bit more above. –  sgtbeano Sep 10 '12 at 14:24
    
My apologies. I have updated this with a working redirect. –  Motoma Sep 10 '12 at 18:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.