-1

setting url rewriting to have nice urls, i have existing urls like that : /xxx/test.php

but in the background, it is allways going to the same script with a query : /xxx/index.php?id=test

with the following rewrite :

RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]

it's working fine.

now, there are old urls still like /xxx/index.php?id=$1 and i want to get rid of these old urls, meaning I want all of them to be for the users like /xxx/test.php with a 301 redirect

i did a rewrite for this but then i'm entering a loop despite the L flag

RewriteCond %{QUERY_STRING} ^id=(.*)$ 
RewriteRule ^xxx/index\.php$ /xxx/%1.php? [R=301,L] 

? is it possible to handle that and how ?

and other to describe it is allways use the script : /xxx/index.php?id=$1 but allways have the right url in the browser displayed

Dave Code
  • 11
  • 1
  • 7

2 Answers2

0

Make your RewriteRule not match index.php or remove the QSA flag.

Say you type test.php well now you will go to index.php?id=test

Then Rewrite occurs again and you will go to index.php?id=index&id=test

Then it will occur again because the page is different: index.php?id=index&id=index&id=test etc.

So add in your regex a negative lookahead: xxx/(?!index)([0-9a-z\-]*)\.php

Try:

RewriteRule ^xxx/(?!index)([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
TheWandererLee
  • 1,012
  • 5
  • 14
  • thanks. for the other regex i did that RewriteCond %{QUERY_STRING} ^id=(.*)$ RewriteRule ^xxx/index\.php$ /xxx/%1.php? [R=301,L] but it is still doing a loop. where is my error ? – Dave Code May 13 '19 at 19:26
  • sorry, not working, still doing the loop, but i did add my other regex to make it clearer – Dave Code May 13 '19 at 19:39
  • your regex should be working but it is not, trying still to understand why it is not. I tried that too : ((?!index)[0-9a-z\-])*\.php and ((?!index)[0-9a-z\-]*)\.php – Dave Code May 13 '19 at 20:22
  • thanks for your help. the 2 regex are workinf separately but not set together – Dave Code May 13 '19 at 20:25
  • I'm re-reading your question and I believe you must do a condition "IF ?id= is present strip it, IF NOT do it the old way" so you can catch this problem that occurs only when using the old links like index.php?id=pagename – TheWandererLee May 13 '19 at 20:29
0

Keep your existing

RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]

which appears to work fine.

Add in these two lines before that which will catch if there is an id= and strip it out of the URL.

RewriteCond %{QUERY_STRING} ^id=([^&]*)(.*)$
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=%1%2 [L,R=301]
  • ^ start of query string
  • ([^&])* any character except &
  • (.*) any following characters

So if query string is id=test&something=else RewriteRule will append exactly that and nothing else as there is no more QSA flag.

Try those 3 lines together (htaccess test website), here is the full htaccess file:

RewriteCond %{QUERY_STRING} ^id=([^&]*)(.*)$
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=%1%2 [L]

RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
TheWandererLee
  • 1,012
  • 5
  • 14