How to properly write a RewriteCond for https and subdomains - regex

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.

Related

htaccess redirect for https, www, no trailing slash on files

I would like to force my urls to https, www and no trailing slashes.
Since I have multiple domains that point to this page, a static domain in there is no option.
This is what i have so far:
# force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# force https
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
This works quite well, except for the case:
https://www.example.com//
which redirects to:
https://www.www.example.com/
Any suggestions on how I can make this work?
Also from a redirects perspective I'm not really sure if this will make 3 redirects in the worst case and can somehow be improved to only one redirect?
Thanks in advance!
In the meantime I found something else which seems to make more sense
# force https and www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
Now the only remaining problems are the following two requests:
http://www.example.com
which redirects to https://www.www.example.com
and
https://www.example.com//
which does not remove the multiple slashes.
any ideas? would really appreciate help!

.htaccess rewrite w/ regular expression and ?lang=en parameter

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.

.htaccess how to redirect subdomain/folder to subdomain only

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]

mod_rewrite .htaccess subdomain redirect

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]

Rewrite URL for ANY subdomain with www to corresponding subdomain without www

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.