1

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>
  • 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
1

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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