简体   繁体   中英

redirect subdomain without changing URL

I got apache2 with mod_rewrite and I also have access to .htaccess of the site.

I want the URL profile.domain.com/username to redirect to www.domain.com/profiles/php?user=username but keep the URL to be profile.domain.com/username . I have already got a wildcard setup to catch all subdomains. I am trying to use:

RewriteCond     %{HTTP_HOST}    ^profile\.domain\.com$ [NC]
RewriteRule     ^(.+)$          http://www.domain.com/profiles/php?user=%1 [R=301,L]

But it changed the URL to http://www.domain.com/profiles/php?user= and never catched the username. Also, I don't want it to change the URL I want it to stay profile.domain.com/username

Your rule is almost correct. %1 is replaced by the first capture group in the last RewriteCond . Since there is no first capture group, it is replaced by.... nothing. You want to use $1 instead. That is replaced by the first capture group in the RewriteRule .

The second mistake you make is making it an external redirect with the R -flag. Remove that flag, and remove the domain-name, and it is treated as an internal rewrite. Make sure you empty the cache of your browser, because it will 'remember' the external redirect you made, and not use the new rewriterules you have.

You'll end up with:

RewriteCond     %{HTTP_HOST}    ^profile\.domain\.com$ [NC]
RewriteRule     ^(.+)$          /profiles/php?user=$1 [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM