Mobile app version of vmapp.org
Login or Join
Debbie626

: Case-specific mod rewrite on WordPress subdomain multisite I have split a WordPress blog into multiple category-specific blogs using subdomains, as the topics in the original blog were too broad

@Debbie626

Posted in: #ModRewrite #Redirects #Subdomain

I have split a WordPress blog into multiple category-specific blogs using subdomains, as the topics in the original blog were too broad to be lumped together effectively.

Posts were exported from the parent www blog and imported into the subject-specific subdomain blogs.

I believe .htaccess provides mod rewrite for all subdomains (including the original www) in a single .htaccess file.

I use .htaccess to perform 301 redirect on post categories to the relevant post on the subdomain's blog e.g.:

RedirectMatch 301 ^/auto/(.*)$ auto.example.com/

The problem I have is that the category has been retained in the permalink structure in the subdomain blog, so that example.com/auto/mercedes is now auto.example.com/auto/mercedes.

The 1st URL is redirect to the 2nd, but unfortunately, the 2nd URL is redirected to auto.example.com/mercedes using the same rewrite rule, which is not found, as the permalink on the subdomain's blog retains the parent category of auto.

The solution would be to adjust the permalink structure in the subdomain's WP settings, so that the top level category does not duplicate the subdomain.

My question would be: how do I then strip a section of the original (www) blog's post URL from the subdomain's URL when redirecting? e.g.: how do I redirect example.com/auto/mercedes to auto.example.com/mercedes?

I'm assuming this would be a regular expression trick, which I am not great at.

Update: I might have to use:

RewriteCond %{HTTP_HOST} !auto.example.com$


In the default WordPress if loop in .htaccess, and seperate my custom subdomain redirections into a second if loop section.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Debbie626

2 Comments

Sorted by latest first Latest Oldest Best

 

@Michele947

Wow, that's a pretty complicated answer. This is how I would do it in an .htaccess context:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} =example.com [NC,OR]
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^(auto|foo|bar)/(.*)$ .example.com// [NS,L,R=301]


The RewriteConds ensure that this rule is only applied for example.com and example.com, not for any other sites. You can remove the first RewriteCond if you only want the rule to apply to example.com.
The RewriteRule then redirects e.g. example.com/auto/mercedes to auto.example.com/auto/mercedes. (If you want it to redirect to just auto.example.com/mercedes, replace / with just .) The example also does the same for the categories foo and bar; you can add as many categories as you wish just by editing the regexp.

10% popularity Vote Up Vote Down


 

@Fox8124981

In ten lines you've got not only what you asked for, but a huge benefit: when you'll want you add a subject e.g "newsubject", just add a line in your map file:

newsubject newsubject


recreate the hash file, restart the server and here you go!!



So here's the "compressed" version:

RewriteMap mapsubdomains
dbm:/web/htdocs/mysite/rewriterules/mapsubdomains.map
RewriteCond %{HTTP_HOST} (([a-zA-Z0-9-]+).)([a-zA-Z0-9-]+).com$
RewriteRule (.*) - [QSA,E=PART1:${mapsubdomains:%1|notfound}]
RewriteCond %{ENV:PART1} ^$ [OR]
RewriteCond %{ENV:PART1} notfound
RewriteRule ^/([a-zA-Z]+)/(.*)$ - [QSA,E=SUBJECT:${mapsubdomains:%1|notfound}]
RewriteCond %{ENV:SUBJECT} !^$
RewriteCond %{ENV:SUBJECT} !(notfound)
RewriteRule ^/([a-zA-Z]+)/(.*)$ %{ENV:SUBJECT}.mysite.com/ [QSA,L]




Detailed answer:

(1) use a map file where you'll write simple stuff like this:

auto auto
moto moto


then include it in your rewrite rule:

RewriteMap mapsubdomains
dbm:/web/htdocs/mysite/rewriterules/mapsubdomains.map


then make a generic rule:

RewriteCond %{HTTP_HOST} (([a-zA-Z0-9-]+).)([a-zA-Z0-9-]+).com$
RewriteRule (.*) - [QSA,E=PART1:${mapsubdomains:%1|notfound}]


The previous rule means: if the hosts has two parts (e.g. part1.part2.com) then look in the mapsubdomain and fill it with the right value or "notfound" if not found.

Then if "notfound" this means you can check for the URL:

# if PART1 empty:
RewriteCond %{ENV:PART1} ^$ [OR]
# ...or PART1 not found:
RewriteCond %{ENV:PART1} notfound
# ...then lookup:
RewriteRule ^/([a-zA-Z]+)/(.*)$ - [QSA,E=SUBJECT:${mapsubdomains:%1|notfound}]

# if subject not empty, and is found then redirect:
RewriteCond %{ENV:SUBJECT} !^$
RewriteCond %{ENV:SUBJECT} !(notfound)
RewriteRule ^/([a-zA-Z]+)/(.*)$ %{ENV:SUBJECT}.mysite.com/ [QSA,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme