The first part of my code works and redirects/rewrites to new/file. Problem occurs when I try to pass variables. They all get redirected to new/file
Whatever variable I try to pass in file.php?foo=bar redirects to new/file instead of new/file/bar.
for example:
- new/file rewrites to file.php
- new/file/2 rewrites to file.php?page=2
- file.php redirects to new/file
- file.php?page=2 doesn’t redirect to new/file/2 but get’s overwritten to new/file instead
My code:
RewriteBase /domain.com
#regular
RewriteCond %{QUERY_STRING} !redirect=no
RewriteRule ^file\.php$ new/file? [NS,R=301,L]
RewriteRule ^new/file?$ file.php?redirect=no [NS]
#with variable
RewriteCond %{QUERY_STRING} ^page=([0-9-]+)/?$
RewriteRule ^file\.php$ new/file/%1? [NS,R=301,L]
RewriteRule ^new/file/([0-9-]+)/?$ file.php?page=$1&redirect=no [NS]
Please note that I am passing more than just page variables.
I’m thinking maybe a QSA flag is supposed to go somewhere?
In line 2 you had ?. This will replace the current querystring, and page=2 is lost.
In line 3 you had a ?. Paths don’t contain questionmarks. Secondly the questionmark is interpreted as an “optional letter ‘e'”, because it is a RegEx. So “/new/fil” would also work (try it out).