url rewriting - How to redirect specific URL to Subdomain

admin2025-01-07  5

I am trying to write a .htaccess rule to redirect the URL to a subdomain.

Example: example/pagecategory/page-singlepagecategory.example/page-single

I've added wildcard subdomain on my hosting.

Can anyone help me write the .htaccess code?

I am trying to write a .htaccess rule to redirect the URL to a subdomain.

Example: example.com/pagecategory/page-singlepagecategory.example.com/page-single

I've added wildcard subdomain on my hosting.

Can anyone help me write the .htaccess code?

Share Improve this question edited Jan 24, 2019 at 15:34 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Jan 24, 2019 at 5:12 hdptrdhdptrd 93 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

To redirect example.com/pagecategory/page-single to pagecategory.example.com/page-single, where pagecategory is entirely variable then you can do something like the following at the top of your .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^([a-z]+)/([\w-]+) https://$1.%{HTTP_HOST}/$2 [R,L]

I've limited the "subdomain" (ie. pagecategory) to just the lowercase characters a-z - this is saved in the $1 backreference. Likewise, $2 holds the "page-single".

Note that this is a 302 (temporary) redirect. Change R to R=301 if this is intended to be permanent, but only once you have confirmed it works as required (to avoid browser caching issues).

you can use a simple 301 redirect in your .htaccess file

RewriteEngine On
Redirect 301 <old_url> <new_url>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254697a226.html

最新回复(0)