简体   繁体   中英

htaccess wildcard subdomain rewrite subfolder causing loop

We are using a wildcard subdomain, *.subdomain.com Each state has its own directory inside the /state/ directory, and within that directory there is a state.html. The URL would be domain.com/state/$state/state.html which would then be rewritten to show domain.com/state.html?state=$state

All works good but now we need to go from subfolder to subdomain so the ending result would be either

$state.domain.com - Preferred but not necessary or $state.domain.com/state.html?state=$state

What we have currently is below, but we get a redirect loop.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain.com
RewriteCond %{HTTP_HOST} ^(.+).domain.com
RewriteRule ^([^/]*)$ /state/%1 [P,L,QSA]

#Rewrite Rule - Force state and city lookup to use state.html or city.html and use info from url to define which city and state.
RewriteRule ^state/(.*)$ state.html?state=$1 [L]
RewriteRule ^city/(.*)/(.*)$ city.html?city=$1&state=$2 [L]

Ended up using the following which works

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain.com
RewriteCond %{HTTP_HOST} ^(.+).domain.com
RewriteRule ^([^/]*)$ /state/%1/state.html?state=$1 [L,QSA]

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