1

I need redirect all requests from subdomain specific directory to main domain

Example:

dev.example.com/directory/test

need to redirect to

example.com/directory/test

(redirect only requests with directory directory)

New contributor
fourston is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Does the subdomain and main domain point to the same area of the filesystem? Is /directory a physical directory? – MrWhite Sep 11 at 14:58
  • perhaps you should read the documentation. What you describe here is a task, not a problem. – asdmin Sep 12 at 9:01
0

In the .htaccess file at the root of your subdomain you can add the following mod_rewrite directives to redirect to the domain apex:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^dev\.(example\.com) [NC]
RewriteRule ^directory/ https://%1%{REQUEST_URI} [R=302,L]

What if i have several subdomains and i wanna redirect it not only from dev.*, but from other subdomains .... Subdomain and main domain point to same directories

To redirect from several specific subdomains then you can modify the above rule to read:

RewriteCond %{HTTP_HOST} ^dev\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^dev0\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^dev1\.example\.com [NC]
RewriteRule ^directory/ https://example.com%{REQUEST_URI} [R=302,L]

Note there is no OR flag on the last condition.

To redirect any subdomain then you can use a more encompassing regex. For example:

RewriteCond %{HTTP_HOST} ^[^.]+\.(example\.com) [NC]
RewriteRule ^directory/ https://%1%{REQUEST_URI} [R=302,L]
  • What if i have several subdomains and i wanna redirect it not only from dev.*, but from other subdomains – fourston Sep 11 at 15:48
  • Where do these subdomains point to? Does the subdomain and main domain point to the same area of the filesystem? Is /directory a physical directory? – MrWhite Sep 11 at 16:49
  • Subdomain and main domain point to same directories For example dev.example.com/directory/test to example.com/directory/test dev0.example.com/directory/test1 to example.com/directory/test1 – fourston Sep 12 at 7:48
  • I've updated my answer. – MrWhite Sep 12 at 13:39

Your Answer

fourston is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.