I want a user who made request from example.com
to mydomain.example/domains
to redirect to mydomain.example/userdomain
. It is like implementing vanity URLs as per the user's domain name.
I know, we can do it using .htaccess
redirect rules but don't know how to do.
I've EDITED the following code as best I can without a way to test it. It should get you close if it doesn't work.
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^/(.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^/(.*) mydomain.example/%1/$1 [L]
should do the trick. Can't test it quickly myself though. There are two key things to notice: You gotta use HTTP_HOST in a RewriteCond to test the domain. Use %1 in the RewriteRule line to get the variable from the HTTP_HOST line in parentheses.
You also gotta redirect any requests to www to the non www with an R=301 permanent redirect first. It might be possible NOT to do this, but I'm not sure at the moment how to extract the non www part of the host regardless of its presence. The R=301 sends a redirect msg back to the browser for anyone who visits www.example.com
. Then the browser will hit example.com
and your second RewriteRule
will match against it. Since its 301 (permanent) the browser will cache the redirect and always do the non-www for the user on its own.
If this doesn't work, compare to some other tutorials on URL redirection and also refer to this wonderful page: http://httpd.apache.org/docs/2.2/rewrite/intro.html