Reputation: 28783
I have rewrite rule that sends all users from random.example.com
to /random/
But I want to make it so that IF a user was to directly visit /random/
it would redirect (redirect NOT rewrite) them to the subdomain.
This is what I have:
RewriteEngine on
# NO WWW
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
# USE RANDOM SUB DOMAIN WITH CONTENT IN SUB FOLDER
RewriteCond %{HTTP_HOST} random.example.com$
RewriteCond %{REQUEST_URI} !^/random
RewriteRule ^(.*)$ /random/$1 [L]
# SEND SNOOPERS AT SUB FOLDER TO SUB
Redirect ^(.*)$/random/$1 %{HTTP_HOST}random.example.com$
However, I get a server 500 error for that last line. Basically it needs to handle any urls that come through on the random directory, so for example:
http://example.com/random -> http://random.example.com/
http://example.com/random/fakeplace/ -> http://random.example.com/fakeplace/
http://example.com/random?id=4 -> http://random.example.com/?id=4
Upvotes: 0
Views: 89
Reputation: 24448
Your syntax is wrong with the last line. You are even mixing RewriteRule with Redirect. Try these rules. I changed a few things.
RewriteEngine on
# NO WWW
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
# USE RANDOM SUB DOMAIN WITH CONTENT IN SUB FOLDER
RewriteCond %{HTTP_HOST} ^random\.example\.com$
RewriteRule !^random/? random%{REQUEST_URI} [NC,L]
RewriteCond %{THE_REQUEST} \s/random/([^\s]*) [NC]
RewriteRule ^ http://random.example.com/%1 [R=301,L]
Upvotes: 1