Wildcard Subdomains, .htacess, and multiple variables

Hello,

I have wild card subdomains set up and have been able put together the rewrite code in the .htaccess file for one variable:

I can successfully redirect:
http://variable.example.com/ to http://example.com/index.php?var=variable
with this code:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^$ /index.php?var=%2 [L]

However, I’m looking to use two variables in the rewrite. I want one variable in as the subdomain and one variable as a sub directory.

Example:
redirect http://variable1.example.com/variable2/index.html
to http://example.com/index.php?var=variable1&id=variable2

I can’t figure out the code to do this. Everything I tried has given me an error. Any help would be appreciated.

How do I write an external redirect for this?

That worked perfectly! Thank you so much!

Change [L] to [L,R=301] :slight_smile:

I see. Of course. What was I thinking. That would be loopy =).

So the two have to be independent of each other. So theoretically, how would I go about it the other way, for curiosity’s sake. That is,

Let’s say a user types in the URL:

How can we get .htaccess to redirect to:
http://nursing.parttimejobsin.us/10000.html

Here, instead of redirecting a wildcard subdomain to a URL, a URL is redirected to a wildcard subdomain.

I’ve tried this, but it doesn’t work:


RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.parttimejobsin\\.us$ [NC]
RewriteRule ^cities/([^/\\.]+)/([^/\\.]+).html?$ http://%2.{HTTP_HOST}/$1.html [R=301,L]

JS,

Have you gone loopy? :lol:

Okay, it appears that you want to show BOTH versions and that’s just not possible. Pick one or the other to show your visitors and just do that.

Back to your question, though. Add the R=301 flag to the Last flag for your RewriteRule as that shows a permanent redirection AND will cause the new {REQUEST_URI} to be displayed.

Redirecting TO the “new format” is something that’s been asked here before so I’ll refer you to my tutorial article for the short answer (and code). You’ll still need to pick one or the other but you can get “loopy” without creating a continuous loop.

Regards,

DK

JS,

What’s an external redirect? Using http?

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.parttimejobsin\\.us$ [NC]
RewriteRule ^$ http://%{HTTP_HOST}/cities/index.php?var=%2 [L]

RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.parttimejobsin\\.us$ [NC]
RewriteRule ^([0-9]+).html$ http://{HTTP_HOST}/cities/index.php?var=%2&id=$1 [L]

Because the redirections will be seen with the absolute redirection using http (or https, for that matter), you may as well change [L] to [R=301,L]. IMHO, redirections like this are normally INTERNAL so that the redirection is not visible to the visitor.

Regards,

DK

Ah! The triumphs of “Specificity!” :Partier:

Regards,

DK

JS,

Okay, that’s clearly NOT what your mod_rewrite is doing!

Spec: Require cities, variable and number as requested from (www.)?domain.us; strip cities and redirect to variable as subdomain retaining number.html. Since you’ve used lowercase exclusively, so will I with my regex:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\\.)?parttimejobsin\\.us$ [NC]
RewriteRule ^cities/([a-z]+)/([0-9]+).html?$ http://$1.parttimejobsin.us/$2.html [R=301,L]

See the difference when you can specify (that’s how I start - and am able to see problems like repeating the {HTTP_HOST} with its www.subdomain when all you needed to see was that the subdomain is NOT present.

Regards,

DK

JS,

You didn’t give your “spec” for the code you showed so allow me to translate:


RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.parttimejobsin\\.us$ [NC]
RewriteRule ^cities/([^/\\.]+)/([^/\\.]+).html?$ http://%2.{HTTP_HOST}/$1.html [R=301,L]

Examine the HTTP_HOST variable and capture the optional www. and ONE OR MORE SUBDOMAIN of parttimejobsin.us (no case - good!). Then match cities/ followed by one or more of any character EXCEPT /, \ or . (the dot character does NOT get escaped in a character range definition) followed by / then the same range of characters followed by ANY CHARACTER (you escaped when you shouldn’t but not when you should) and html. Whew! Okay, if the matches were made, redirect to http://subdomain.www.subdomain.parttimejobsin.us/$1 (first set of characters, not the filename for the html file. Do you see where you went wrong (other than escaping the wrong dot characters)? DOMAIN then … well, did you want nursing or 10000?

Okay, what IS your spec?

Regards,

DK

Specs-

Rewrite URL:
http://domain.us/cities/variable/number.html

To:
http://variable.domain.us/number.html

I’m not sure if this is called an external redirect, but in order to not be penalized for duplicate content I’d like for when a user types in the URL:

the content displayed will be from:

(This has been successfully implemented with the code in the previous posts)

However, if a user types in the URL:

Then, their URL will automatically rewrite to:

Is that possible?

Simple!

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.example\\.com$ [NC]
RewriteRule ^$ /index.php?var=%2 [L]

[B]RewriteCond %{HTTP_HOST} ^(www\\.)?([^\\.]+)\\.example\\.com$ [NC]
RewriteRule ^(.*)$ /index.php?var=%2&id=$1 [L][/B]
# BECAUSE (.*) will match index.php,
# change (.*) to ([a-zA-Z]+) or
# ANY character range definition
# without a dot character else
# add another RewriteCond to exclude
# index.php from the {REQUEST_URI}

Easy enough for you?

Regards,

DK

Thanks alot!

I ended up using this code:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.parttimejobsin\.us$ [NC]
RewriteRule ^$ /cities/index.php?var=%2 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.parttimejobsin\.us$ [NC]
RewriteRule ^([0-9]+).html$ /cities/index.php?var=%2&id=$1 [L]