Apache rewrite based on subdomain - apache

I'm trying to redirect requests for a wildcard domain to a sub-directory.
ie. something.blah.example.com --> blah.example.com/something
I don't know how to get the subdomain name to use in the rewrite rule.
Final Solution:
RewriteCond %{HTTP_HOST} !^blah\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^(.*) /%1/$1 [L]
Or as pointed out by pilif
RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.example\.com$

You should have a look at the URL Rewriting Guide from the apache documentation.
The following is untested, but it should to the trick:
RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.domain\.com$
RewriteRule ^/(.*)$ http://blah.domain.com/%1/$1 [L,R]
This only works if the subdomain contains no dots. Otherwise, you'd have to alter the Regexp in RewriteCond to match any character which should still work due to the anchoring, but this certainly feels safer.

Try this:
RewriteCond %{HTTP_HOST} (.+)\.blah\.domain\.com
RewriteRule ^(.+)$ /%1/$1 [L]
#pilif (see comment): Okay, that's true. I just copied a .htaccess that I use on one of my projects. Guess it has a slightly different approach :)

#Sam
your RewriteCond line is wrong. The expansion of the variable is triggered with %, not $.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.media\.xnet\.tk$
^
that should do the trick

Related

three conditions and one rule to use any domain name in htaccess file

I have these conditions and rule in my htaccess file, and it works and does what it's supposed to do.
RewriteCond %{REQUEST_URI} !^/index/parser.php?(.*)?$
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.extension$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.mydomain\.extension [NC]
RewriteRule ^(.*)$ http://mydomain.extension/index/parser.php?%2&domain_name=mydomain.extension [QSA,L]
I want to rewrite it so it can work with any domain name pointed at the server, but don't know how. I tried this but it failed. It should only activate on subdomains, not root domains. Also, I am not versed on the whole dollar sign and percentage usage in htaccess files. $1, $2, %1, %2, etc. Never know when dollar or percent should be used, or what number to use after it.
RewriteCond %{REQUEST_URI} !^/index/parser.php?(.*)?$
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.(.*) [NC]
RewriteRule ^(.*)$ /index/parser.php?%2&domain_name=%1 [QSA,L]
Edit
Tried this on last line. Still didn't work.
RewriteRule ^(.*)$ http://%1/index/parser.php?%2&domain_name=%1 [QSA,L]
Edit
Tried suggestion and it made this:
something.tld
Go to this:
http://tld/index/parser.php?something&domain_name=tld
Should be:
http://something.tld/index/parser.php?something&domain_name=something.tld
Edit
The next version didn't work either. Showed an internal server error page. Same error whether domain.tld was used or subdomain.domain.tld was used.
How about:
RewriteCond %{REQUEST_URI} !^/index/parser.php
RewriteCond %{HTTP_HOST} ^(?:www\.|)([a-z0-9-]+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ http://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
So this will take a request for:
http://www.foo.some-domain.tld/anything
and redirect to:
http://some-domain.tld/index/parser.php?foo&domain_name=some-domain.tld
As far as the backreferences go, the $ ones backreference groupings made in the rewrite rule, while the % ones backrefernce groupings from a rewrite condition.
See: http://httpd.apache.org/docs/trunk/rewrite/intro.html#regex

Mod ReWrite to remove component of URL

I must be an idiot because I just can't work this bit out.
I've got a URL:
www.site.com.au/products/product-name.html
I need to redirect these to:
www.site.com.au/product-name.html
All the links are dynamic, the folder doesn't exist. What ReWrite rule do I use to accomplish this?
This is what I've got so far:
RewriteCond %{HTTP_HOST} ^(www|test)\.site\.com\.au
RewriteCond %{REQUEST_URI} ^(/products/)
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^.+\.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L]
Just need to add the bit to remote /products
Thanks.
RewriteRule ^products(/.*)$ http://www.site.com.au$1 [L, R=301]
This replaces everything you listed, except for the first RewriteCond (to match the domain, though if your VirtualHost only answers on those two domains, you can exclude that RewriteCond to simplify it).
RewriteRules are matched first before Apache looks at the RewriteConds, so if you can do the match in the RewriteRule itself it greatly simplifies things. Just for your future reference, if you did need to match in the RewriteCond, it would look something like this:
RewriteCond %{REQUEST_URI} ^/products(/.*)$
RewriteRule ^.*$ http://www.site.com.au%1 [L, R=301]
Note the %1 for matching what's in the parentheses in the RewriteCond vs. the $1 for matching what's in the RewriteRule.
EDIT: Per your comment, the following modification should force lowercase. I haven't had to do that myself, but per this Apache documentation it's an internal function via RewriteMap. Based on your original code it looks like maybe you already have the RewriteMap definition elsewhere. If not, I've included it here.
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} ^/products(/.*)$ [NC]
RewriteRule ^.*$ http://www.site.com.au${lc:%1} [L, R=301]
OK, I really don't know enough about Apache Rewrite to figure out the exact formatting. But after much ado these are the results that worked:
# Lowercase all /products/
RewriteCond %{HTTP_HOST} ^(www)\.site\.com\.au
RewriteCond %{REQUEST_URI} ^/products/.+\.html
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^ ${lc:%{REQUEST_URI}} [R=301,L]
# Lowercase all /products/ and strip products/ subfolder
RewriteCond %{HTTP_HOST} ^(www)\.site2\.com\.au
RewriteCond %{REQUEST_URI} ^/products/.+\.html
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^products/(.+\.html)$ /${lc:$1} [R=301,L]
Thanks,
Dom

Apache RewriteCond seems to be ignored?

I have the following code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/wp-admin
RewriteRule ^(.+)$ http://example.com/$1 [R=permanent,L]
I have tried various variations of above, but currently the wp-admin rewrite condition is ignored.
I don't want "/wp-admin" directory and "/" root requests redirected! (Yes, I know, sounds funky)
What am I doing wrong in above mod_rewrite?
My bad.
For anyone in similar situation, you also need
RewriteCond %{REQUEST_URI} !^/wp-login
(since wp-admin redirects to wp-login)

RewriteCond not working to remove www. from subdomain URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.new.domain.com [NC]
RewriteRule ^(.*)$ http://new.domain.com/$1 [L,R=301]
why when i enter http://www.new.domain.com is not redirected to http://new.domain.com?
The .htaccess file is in the right folder (it has more rewritecond's and they work)
The regexp syntax in your RewriteCond is slightly broken: the correct way to test for strict equality with www.new.domain.com (up to differences in case) is either
RewriteCond %{HTTP_HOST} ^www\.new\.domain\.com$ [NC]
or
RewriteCond %{HTTP_HOST} =www.new.domain.com [NC]
That said, those errors should not stop your rewrite rule from working: you original RewriteCond will match www.new.domain.com just fine, it just matches some other strings too (like wwwXnewYdomainZcomFOOBAR). In fact, I have a very similar set of rules in my own .htaccess file, and they work just fine:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^vyznev\.net$
RewriteCond %{HTTP_HOST} ^(www\.)?vyznev\.net$ [NC]
RewriteRule ^(.*) http://vyznev.net/$1 [NS,L,R=permanent]
Most of the differences between your code and mine are purely cosmetic. The only potentially significant issue I can see if that you don't have a RewriteBase directive; you should definitely add one, if only to avoid potential problems later. Still, as far as I can tell, not having one shouldn't stop you from getting at least some redirect, even if it might not be to the URL you expect.

Why would this .htaccess be stripping out containing folder?

This is from a .htaccess located under /~new/
# invoke rewrite engine
RewriteEngine On
# force domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$0 [R=301,L,NC]
When accessing http://domain.com/~new/hello, it is being rewritten to http://www.domain.com/hello
The www. is being added in like it should, but for some reason it is ignoring the /~new/ subdirectory.
Does anyone know what may be causing this? FYI, there is a .htaccess in the TLD but it is empty. I know I could tack on /~new/ to the regex replacement string, but I'd prefer a generic solution (for portability) and I am not sure why it's stripping it out in the first place. I have also tried playing around with RewriteBase but could not get it to work.
Thanks
I think normally the {REQUEST_URI} would be in your rewrite rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
RewriteRule ^.*$ www.%1.%2%{REQUEST_URI} [R=301,L]