Htaccess redirct subdomain with last url parameter

I want to write a .htaccess file to redirect my subdomains and the last URL variable to a new location. Here is what I want to do:

HTTP (S): //abc.example.com/books

I want my internal URL to be as follows:

http://example.com/?name=abc&type=books

I already got a subdomain redirect to work, but I can’t do a subdomain with a variable in the last part of the URL.

How can i do this?

+2
source share
2 answers

This should do what you want:

RewriteCond %{HTTP_HOST} ^(.+).example.com RewriteRule ^(.*)% http://example.com/?name=%1&type=$1 [R,L] 

"% 1" means using the first capture group from RewriteCond above.

+4
source
 RewriteCond %{HTTP_HOST} ^(.+)\.example\.com RewriteRule ^([^/]*)$ http://example.com/?name=%1&type=$1 [R,L] 

"%1" means using the first capture group from RewriteCond, and $1 means the first capture group in the rule itself.

In your example, %1 will be "abc" and $1 will be a "book"

[^/]* means "match each character with no slash, 0 or more times

+2
source

All Articles