This seems easy enough but I can't seem to find the exact solution online and to get it to work. I want to redirect all the links e.g.
user1.example.com
user2.example.com
user3.example.com etc.
to
example.com/search.html?user=user1
example.com/search.html?user=user2
example.com/search.html?user=user3 etc.
in my .htaccess I have
RewriteCond %{HTTP_HOST} !^(www\.)?example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([\.]+)\.example.com$ [NC]
RewriteRule .* /search.html?user=% [L]
I want to redirect all the user "subdomains" (they don't really exist on the server) except if the domain is prefix with www. The "user" field consists of alphabets, numeric, - (dash), _ (underscore) and possibly space. I am getting the Server Not Found error with the above .htaccess with for example, user1.example.com What am I doing wrong? Can this be done without the real existence of these third level domains? Thanks in advance.
You can use negative lookahead in your condition while capturing first part of domain and use the back-reference as %1 later:
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$ [NC]
RewriteRule !^search\.html$ /search.html?user=%1 [L,QSA,NC]
Try this :
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule .* /search.html?user=%1 [R,L]
Related
I have a typo3 installation with multidomain and multilanguage, where not every language is setup to every domain.
Languages are de/fr/en/pt/es/cn/
www.example.de can be de/en/fr but not
www.example.de pt/es/cn
now I have messed up sth. and google has indexed loads of wrong urls e.g.
www.example.de/pt/
www.example.de/es/
www.example.de/cn/
they point to languages that are not set for this domain
I am fiddling around in the htaccess to redirect 301 the wrong urls with wildcards(?) to the .tld
I am looking for a solution redirect eg.
www.example.de/pt/* to www.example.de/
www.example.de/es/* to www.example.de/
www.example.de/cn/* to www.example.de/
where the * should represent the complete sting/path following the language parameter.
and of cause the same procedure for the .com domain
www.example.com/fr/* to www.example.com/
www.example.com/de/* to www.example.com/
I searched the web up and down but nothing I tried works.
any help would highly apreciated.
a tiny step further
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^cn/(.*)$ http://www.example.de/ [L,R=301]
RewriteRule ^pt/(.*)$ http://www.example.de/ [L,R=301]
RewriteRule ^es/(.*)$ http://www.example.de/ [L,R=301]
this seems to work
and now for the second domain as I need to differentiate between .com and .de
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^de/(.*)$ http://www.example.com/ [L,R=301]
RewriteRule ^fr/(.*)$ http://www.example.com/ [L,R=301]
this now breaks www.example.de/fr/whatever and redirects it to www.example.com as well.
so it looks like the first condition is matching and the the last rule is applied for french.
how can I limit the rules assigning them only to the appropriate domain conditions?
ok looks like every condition and respective rule needs to be written in a single line an repeated
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^cn/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^pt/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^es/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^de/(.*)$ http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^fr/(.*)$ http://www.example.com/ [L,R=301]
this seems to work.
any better solution is highly appreciated
I have built a multilingual site in Joomla! 3.1.x Dutch and English, using the multilingual functions native to Joomla! 3.1.x. I have two domain names that I want to go to this site, one to the Dutch side, the other to the English side.
http://www.internationalerozekerk.nl
http://www.internationallgbtchurch.org
Number 1 should go to: index.php?lang=nl
Number 2 should go to: index.php?lang=en
In the .htaccess I have added this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.org [NC]
RewriteRule (.*) ^internationalerozekerk.nl/index.php?lang=en$1 [L,R=301]
This redirects the English URL to internationalerozekerk.nl/index.php?lang=en. However, the address bar still reads: internationalerozekerk.nl/index.php?lang=en and not internationallgbtchurch.org
I haven't found anything to make the two URLs stay in the address bar.
Any suggestions?
Thanx,
Thom
You can use these rules instead for internal redirects:
RewriteCond %{HTTP_HOST} ^(www\.)?internationallgbtchurch\.org$ [NC]
RewriteRule ^$ /index.php?lang=en [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?internationalerozekerk\.nl$ [NC]
RewriteRule ^$ /index.php?lang=nl [L,QSA]
Reference: Apache mod_rewrite Introduction
Try removing the hostname and the R flag and remove the $1.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.org [NC]
RewriteRule ^(.*)$ /index.php?lang=en [L,QSA]
For the other site:
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.nl [NC]
RewriteRule ^(.*)$ /index.php?lang=nl [L,QSA]
Adding the $1 at the end appends the request URI, which will completely mess up the query string. If you want the path to be sent as another param, then you need to be explicit about it (for example, "path"):
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.org [NC]
RewriteRule ^(.*)$ /index.php?lang=en&path=$1 [L,QSA]
I am using the following code to redirect wildcard subdomains (*.domain.com) to their coresponding folder in /users and redirect direct requests to the /users folder to the subdomain version:
Protect Direct Access to Wildcard Domain Folders:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
Handle Wildcard Subdomain Requests:
RewriteCond %{REQUEST_URI} !^/users/ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^(.*)$ /users/%1/$1 [L]
This code works well enough, however there are two problems that I can't seem to fix.
The below scenario seems to happen because there isn't a trailing slash on the requesting URI:
username.domain.com/sub1 => username.domain.com/users/username/sub1
username.domain.com/sub1/ => username.domain.com/sub1/
The users directory can still be accessed directly by using a subdomain:
username.domain.com/users/username/sub1 => Works and shouldn't
I'm at a loss and would really appreciate if anyone has any ideas.
Thank you!
For the problem 2, I think your first protection rule just needs to redirect all subdomains. It's redirecting www, but lets username.domain.com come through as-is.
This will redirect any direct access request to the users path.
RewriteCond %{HTTP_HOST} ^.+\.domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
I think it can be a little simpler by just looking for any host ending in domain.com (which would even handle no subdomain, just domain.com) (I didn't test this....)
RewriteCond %{HTTP_HOST} domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
For problem 1, I'm stumped too, sorry. It's acting like the trailing slash is failing the rules, so it falls through as-is. But I would expect it to do as you want it to:
username.domain.com/sub1/ => username.domain.com/users/username/sub1/
Perhaps try the %{REQUEST_URI} tag, instead of trying to capture .*. I don't see the difference, but maybe it'll help.
RewriteCond %{REQUEST_URI} !^/users/ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule .* /users/%1%{REQUEST_URI} [L]
I am trying to create a mod_rewrite rule to direct people to a sub-folder. Currently the code looks as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
The redirect works if the user types www.abcsite.com, but not if they type abc.com. Is there something that I am missing or should do differently to make sure the user goes to the correct folder (regardless of how they type the URL)?
Side note: The htaccess file that I am dealing with is a Joomla file, so all contents of it deal with another Joomla site. I appreciate the help.
Because you have conditions for that.
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
All above rules will pass only its abcsite.com
You add following rules also then it work for abc.com too.
RewriteCond %{HTTP_HOST} abc.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abc\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
There's a stray ! in your second condition. A ! in front of the pattern means that the condition is true when the regex doesn't match (like in the third condition). A ! inside the pattern is just a literal symbol.
The host conditions should be something like:
RewriteCond %{HTTP_HOST} ^abcsite\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.abcsite\.com$ [NC]
And in fact, they can be joined into a single condition (note, no [OR] here):
RewriteCond %{HTTP_HOST} ^(www\.)?abcsite\.com$ [NC]
Your third condition is intended to prevent redirect loops (/foo → /abc/foo → /abc/abc/foo → …). What it says is that the rule isn't applied if the request URL starts with /abc/. However, your actual redirect is an internal redirect: if a user accesses abcsite.com/foo, the server internally rewrites this to /webroot/abc/foo, but REQUEST_URI stays the same, /foo.
The reason this doesn't cause a redirect loop as it is is likely rewrite rules in abc/.htaccess which override this one once the redirect is done.
What should be checked instead in the third condition is the path matched by the rewrite rule:
RewriteCond $1 !^abc/
RewriteRule (.*) /abc/$1 [L]
I am looking to make www.purchase.example.com redirect to purchase.example.com, below is an example of what I am trying to do:
RewriteCond %{HTTP_HOST} ^www\.purchase\.
RewriteRule (.*) http://purchase.DOMAIN_NAME/$1 [L,R]
I need a variable that will replace DOMAIN_NAME with simply purchase.example.com.
Obviously I can hard code the purchase.example.com but I will need the code to work on multiple sites. Any suggestions?
For your knowledge:
I used a RewriteCond backreference:
RewriteCond %{HTTP_HOST} ^www\.purchase\.(.*)
RewriteRule (.*) http://purchase.%1/$1 [L,R]
I would not do this in code, I would do this on the web hosting account.
If you need it a little more generic redirect for every domain starting with www.:
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ http://%0%{REQUEST_URI} [L,R=301]