Search code examples
regex.htaccess

Redirect URI Path to Subdomain With Regex


How can i redirect this URI to subdomain with RegEx? I need to redirect only this file.php with query string.

https://url.com/file.php?url=anystring
to
https://sub.url.com/file.php?url=anystring

i tried this, didn't work.

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^\/file\.php$ 
RewriteCond %{QUERY_STRING} ^url=([a-z]*) 
RewriteRule ^ https://sub.%1%{REQUEST_URI} [R=301,L]

Solution

  • RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/file.php$ 
    RewriteCond %{QUERY_STRING} ^url=([a-z]*) 
    RewriteRule ^(.*)$ https://sub.url.com/file.php [R=301,L]