I am working on a subdomain where I need to redirect all pages to the main domain except a few pages.
For example, http://subdomain.domain.com/contact-us
and http://subdomain.domain.com/thank-you
pages should not redirect. All other pages should be redirected to http://www.domain.com
.
The main domain is hosted elsewhere and the subdomain is pointed to the server I have access to. In the subdomain's .htaccess, can I have something like this: RewriteEngine On RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)/?!(contact-us|thank-you)$ http://www.domain.com/$1 [R=301,L]
You were close:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(?!(?:contact-us|thank-you))(.*)/?$ http://www.domain.com/$1 [R=301,L,NE]
The negative lookahead (?!(?:contact-us|thank-you))
ensures that what follows is neither contact-us
nor thank-you
.
Reference
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.