0

so I've recently changed my website's file structure a little bit and now need to redirect my old URLs to the new ones.

This is what the old URLs looked like:
example.com/script?id=1&title=TitleHere

the script.php is now in a sub-folder with the name "script", so the new URL is:
example.com/script/1/TitleHere (This one is already working, but I still need my old urls to redirect to this pattern.)

This is what I tried in my htaccess:
RewriteRule ^script?id=([0-9]+)&title=(.*) /script/$2-$1 [L,R=301]

Not sure if this is of importance but I am also using these htaccess settings:

Options -Indexes 
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

Sadly my attempt didn't bring me any success, it's just throwing a 403 and I think that's because a folder named "script" already exists but I am not sure. Help would be highly appreciated!

xyba
  • 17
  • 6
  • RewriteRule matches against the path component of the URL only, you can not use it to check on query string contents - for that, you need to use a RewriteCond. – CBroe May 06 '22 at 06:45
  • Thank you so much for your reply, this definitely got me closer to solving my issue! By using the following code the URL looks a bit weird but it at least works: `RewriteCond %{REQUEST_URI} ^/script.php [NC] RewriteCond %{QUERY_STRING} ^id=([0-9]+)&(.*) RewriteRule (.*) http://example.com/script/%2-%1? [QSA,R=301,L]` Now the issue is, that all my old URLs didn't use .PHP at the end and when I use the above code without the .PHP it seems to redirect the users in a loop, causing it to error. Is there any workaround for this scenario? @CBroe – xyba May 06 '22 at 13:11
  • Remove the first RewriteCond, and make your RewriteRule pattern `^script$` instead. By anchoring it at the end as well with the `$`, it won't allow the URL path to consist of anything more than that, so it won't match your new URL paths of the form `script/...` any more. – CBroe May 06 '22 at 13:18
  • Thank you so much, I got it fully working now. You're a lifesaver. Have a wonderful rest of your day. – xyba May 06 '22 at 13:53

0 Answers0