Monday, March 28, 2011

How to move a char from the subdomain to the filename in Apache mod rewrite?

I have this URL:

http://hostX.site.com/some_path_here/filename.jpg

and need to rewrite it to:

http://host.site.com/same_path_here/filenameX.jpg

Can you please tell me if this is possible? Basically I am trying to move "X" (it is a number) from the subdomain to the end of the filename, just before the extension.

Thanks.

From stackoverflow
  • (Without testing,) something like this should work:

    RewriteCond %{HTTP_HOST} host(\d).site.com
    RewriteRule (.*)\.(.*) host.site.com/$1%1.$2
    

    You need to extract the host number through the RewriteCond. In the RewriteRule you can use the number with the %-modifier

    HTH

  • Something like this should do it:

    RewriteCond %{HTTP_HOST} ^host(\d)\.example\.com$
    RewriteRule (.*)\.([^/.]+)$ http://host.example.com/$1%1.$2
    

    But since the host is different, this will cause an external redirect.

0 comments:

Post a Comment