0

I need a .htaccess expression to use the second get variable or get the get variable by name. Currently my mod_rewrite alters the URL to remove index.php filename and alter the pageName GET variable so that it just looks like a filename.

I now need a second GET variable, but currently the way it is, it does not even recognise get variables after the first rewrite, so I would like the mod_rewrite to handle the second var which will be 'blogpage=1'(1 will be the blogpage that you are currently on) so that it will look like so:

*********.co.uk/PageName/2

instead of this

*********.co.uk/PageName?blogpage=2

As this does not currently work, the PHP script simply thinks that the var is not present and returns a NULL value.

This is my current mod_rewrite conditions:

RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^([^/\.]+)/?$ index.php?pageName=$1
Johannes H.
  • 272
  • 2
  • 11

1 Answers1

1

Apache will replace the entire query string if the rewrite rule specifies a new one. TO append instead, use the QSA flag, as noted in the mod_rewrite documentation:

Modifying the Query String

By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

So, if you change your code to

RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^([^/\.]+)/?$ index.php?pageName=$1 [QSA]

it should forward the old query string as well.


To do the second rewrite, just add either another rule with another capturing group.

RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^([^/\.]+)/([0-9]+)$ index.php?pageName=$1&blogpage=$2 [QSA,L]

RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^([^/\.]+)/?$        index.php?pageName=$1 [QSA]
Johannes H.
  • 272
  • 2
  • 11