I spend the last 2 days searching and learning about .htaccess to redirect all subdomains to a user folder
without changing the url, but i keep having 3 little problems. I really would appreciate some help
problem 1)
If i go to http://www.example.com/users/foo it automatically redirect
the url to http://foo.example.com/ but if i go to http://www.example.com/users/foo/dashboard.php
it does nothing, instead of redirecting to http://foo.example.com/dashboard.php
this is the code i use:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /users/(.+)/\ HTTP/
RewriteRule ^users/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
problem 2)
if i go to http://foo.example.com/dashboard.php
it gets its information from http://www.example.com/users/foo/dashboard.php
but if i type foo.example.com in the address bar (in firefox) it (sometimes) automatically redirect
to www.foo.example.com, can i prevent the www prefix?
this is the code
RewriteCond %{REQUEST_URI} !^/users/
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule (.*) /users/%1/$1 [L]
problem 3)
the campaign website is locaded in the folder http://www.example.com/website but should be
visible on the url http://www.example.com/ (like i get the information from http://www.example.com/users/foo
on the url http://foo.example.com/).
About problem one I would just check the host:
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^users/([^/]+)/(.*)$ http://$1.domain.com/$2 [R=301,L]
About problem second this is a modified rule which I got from the drupal .htaccess file:
# remove www prefix
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com$ [NC]
RewriteRule ^ http://%1%.domain.com{REQUEST_URI} [L,R=301]
# redirect to the subdirectory
RewriteCond %{HTTP_HOST} !^www\.(.+)\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteRule ^ user/%1%{REQUEST_URI} [L,R=301]
About your third problem check this one here:
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ website/$1 [R=301,L]
Related
I need a hand with creating a regex rewrite rule for my .htaccess.
Here's the problem. My previous URLs structure was:
http://example.com/whatever-the-url-is/?lang=en
now I turned it into
http://example.com/en/whatever-the-url-is/
I'd like to create an .htaccess URL that 301 redirects all the URLs from the previous structure to the new one.
Also the .htaccess is shared between different domains, and so there should be
RewriteCond %{http_host} !^example.com$ [NC] condition at the beginning...
Is it possible? Do you have any suggestions?
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(.+?)(?:&|$) [NC]
RewriteRule ^(.*)$ /%1/$1? [R=301,L]
You can add the RewriteRule after RewriteCond %{HTTP_HOST}:
RewriteCond %{REQUEST_URI} !^/(?:en|fr|sp)/ [NC]
Where you test if langcode is already in the url.
Thus far I've gotten this far with my RewriteConditions but it's from hacking and slashing from internet examples. They seems to behave a little funny at times, like showing the directory in the url, so I assume I'm not doing it right.
The desired behaviour: I have multiple subdomains all pointing to the same root. Based on the subdomain value I want to redirect to a sub-folder in the root. I also want to force HTTPS. I have multiple subdomains, but I'll only show two for the sake of brevity. All the other conditions are virtually identical.
Here's my .htaccess code:`
RewriteEngine On
RewriteCond %{HTTP_HOST} purchase.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !purchase/
RewriteRule (.*) https://purchase.mydomain.com/purchase/$1 [L]
RewriteCond %{HTTP_HOST} booking.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !booking/
RewriteRule (.*) https://booking.mydomain.com/booking/$1 [L]
`
Thank you,
Mike
Change your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(purchase|booking)\.mydomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} =purchase.mydomain.com
RewriteRule ^((?!purchase/).*)$ /purchase/$1 [L]
RewriteCond %{HTTP_HOST} =booking.mydomain.com
RewriteRule ^((?!booking/).*)$ /booking/$1 [L]
http:// or https:// in target will redirect instead of rewriting.
I am hosting multiple domains on one hosting account, so I have made folders in the public_html folder for each domain.
e.g. home/username/public_html/examplewebsite.com/
and
home/username/public_html/otherexamplewebsite.com/
I've been trying to figure out the code I need in my htaccess file to redirect visitors to the appropriate subfolder when they type in examplewebsite.com so that they don't see that they're in a subfolder of the main domain. I found this code originally, which was working:
Options -Indexes +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/domainfolder/
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.
#Rewrite all those to insert /folder
RewriteRule ^(.*)$ /domainfolder/$1 [L]
but I also want to redirect all visitors who type in the naked domain to the www. version. I found this code, but when I use them both in the htaccess file you can see the subfolder name in the url again:
#Only www, no naked domain
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
I don't really understand much of all this code so If any of you gurus could help me figure out a solution I would really appreciate it!
Keep your rules like this in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\. [NC]
RewriteRule !^domainfolder/ domainfolder%{REQUEST_URI} [L,NC]
I'm trying to find an answer on different places, but I can't find the full solution for this.
I had a folder setup for a blog. Now we move to an subdomain setup. The redirect shown below works perfectly. The only thing that doesn't work is when the url is a subdomain combined with folder. that isn't redirected.
RewriteCond %{HTTP_HOST} ^(www\.)domain\.com$
RewriteRule ^blog/(.*)$ http://blog.domain.com/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteCond %{REQUEST_URI} !blog/
RewriteRule ^(.*)$ blog/$1 [L,QSA]
www.domain.com/blog/some-url-here is redirected to
blog.domain.com/some-url-here
When I try blog.domain.com/blog/some-url-here it returns a http-status of 200 and when I try to redirect I get a infinitive loop.
Is there a redirect that I've missed?
Try these rules on top of your other rules:
RewriteCond %{THE_REQUEST} \s/blog/([^\s]*) [NC]
RewriteRule ^ http://blog.domain.com/%1 [L,R=301]
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteRule !^blog/ blog%{REQUEST_URI} [L,NC]
We have a multi-subdomain site that generates dynamic cotent depending on the subdomain text. However it does not work if www is appended to the subdomain. As some users are used to add www in front of every URL, we would like to fix it with a URL rewrite.
EDIT
I have got this far:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.subdominio\.dev [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{http_host} ^www\.([^\.]+)\.subdominio\.dev [NC]
RewriteRule ^(.*) http://%1.subdominio.dev$1 [R=301,QSA,NC]
Surprisingly. It works well in one of my test subdomains, but not in the other:
www.otro-mas.subdominio.dev gets redirected to otro-mas.subdomino.dev (with and without an URI like /index.html). Just as expected.
However www.ono.subdominio.dev is going into an infinite redirect. Like this:
http://www.ono.subdominio.dev/ono.subdominio.dev//ono.subdominio.dev//ono...
Why is it not rewriting the host?
Try this:
RewriteCond %{HTTP_HOST} ^www\.([a-zA-Z-_]+)\.domain\.com [NC]
RewriteRule ^(.*)$ http://%1\.domain\.com/$1 [R=301,NC,QSA,L]
You probably forgot the QSA and the L directives (search on the Apache mod_rewrite documentation for the explanation).
It does work.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.subdominio\.dev [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{http_host} ^www\.([^\.]+)\.subdominio\.dev [NC]
RewriteRule ^(.*) http://%1.subdominio.dev$1 [R=301,QSA,NC]
I had some problem with the caches in my browser.