htaccess Multiple Conditions for RewriteRule - .htaccess

Is there any way to reduce these lines?
RewriteCond %{HTTP_HOST} =subtest1.test.de
RewriteRule ^robots\.txt$ /robots_testing.txt [L]
RewriteCond %{HTTP_HOST} =subtest2.test.de
RewriteRule ^robots\.txt$ /robots_testing.txt [L]
RewriteCond %{HTTP_HOST} =subtest3.test.de
RewriteRule ^robots\.txt$ /robots_testing.txt [L]
I want subtest[1-3].test.de all to use robots_testing.txt.
It already works, but is there a way to combine the conditions?

RewriteCond %{HTTP_HOST} =subtest1.test.de [OR]
RewriteCond %{HTTP_HOST} =subtest2.test.de [OR]
RewriteCond %{HTTP_HOST} =subtest3.test.de
RewriteRule ^robots\.txt$ /robots_testing.txt [L]
or even (but I find this less readable)
RewriteCond %{HTTP_HOST} ^subtest[1|2|3]\.test\.de$
RewriteRule ^robots\.txt$ /robots_testing.txt [L]

Related

How to change document root of a website and enforce "https" only?

I am choosing document root via .htaccess in a following manner -
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
How can enforce all requests to https too ?
Try this :
RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
Note clear browser cache the test

SEO, joomla and 2 domains for 2 languages

I’ve been looking around for the answer but I can't seem to find it. This is what I need:
www.domain1.it/en -> www.domain2.com/en
www.domain2.com/it -> www.domain1.it/it
I have tried a lot of possible solutions but no one works
RewriteCond %{HTTP_HOST} www.domain2.com
RewriteCond %{REQUEST_URI} ^/it/ [NC]
RewriteRule .* http://www.domain1.it/it [R=301,L]
It doesn’t work:
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteCond %{REQUEST_URI} ^/it/(.*)$
RewriteRule ^(.*)$ http://www.domain1.it/$1 [L,R=301]
It doesn’t work:
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/it/(.*)$
RewriteRule ^(.*)$ http://www.domain1.it/$1 [L,R=301]
It doesn’t work:
RewriteCond %{HTTP_HOST} www.domain2.com
RewriteCond %{REQUEST_URI} ^/it/
RewriteRule ^(.*)$ http://www.domain1.it/it/ [R=301,L]
It doesn’t work:
RewriteCond %{HTTP_HOST} ^www\.domain2\.com
RewriteCond %{REQUEST_URI} ^/it/
RewriteRule ^(.*)$ http://www.domain1.it/$1 [R=301,L]
It doesn’t work:
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond $1 !^en/$ [NC]
RewriteRule ^(.*)$ http://www.domain1.it/$1
I actually think some of your attempts would work, but your problem is probably your browser caching redirects making your debugging unreliable.
I think the following redirects are simpler and will do what you want.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$ [NC]
RewriteRule ^it(.*) http://www.domain1.it/it$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.domain1\.it$ [NC]
RewriteRule ^en(.*) http://www.domain2.com/en$1 [R=301,L]

dynamic subdomains using .htaccess in php

i am trying to implement dynamic
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^/?$ /live/agent/index.php?agent_user_name=%1 [L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^(.+?)/?$ /live/agent/$1.php?agent_user_name=%1 [L]
Now this .htaccess converts
http://mydomain.com/live/agent/index.php?agent_user_name=username to http://username.mydomain.com
and
http://mydomain.com/live/agent/forum.php?agent_user_name=username to http://username.mydomain.com/form/
However, there are other pages as well which i want to redirect to subdomain like
http://mydomain.com/live/agent/view_blog.php?agent_user_name=username&blog_id=19 this page should be read via subdomain something like http://username.mydomain.com/view_blog/19 etc and also
http://mydomain.com/live/agent/page.php?agent_user_name=username&content_id=19 this page should be accessed by http://username.mydomain.com/content/19 etc
Thanks
This should work:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^content/([0-9]+)/?$ /live/agent/page.php?agent_user_name=%1&blog_id=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^([^/]+)/([0-9]+)/?$ /live/agent/$1.php?agent_user_name=%1&blog_id=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^/?$ /live/agent/index.php?agent_user_name=%1 [L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^(.+?)/?$ /live/agent/$1.php?agent_user_name=%1 [L]
Add the following below your current rules should achieve what you want.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /live/agent/view_blog.php\?agent_user_name=(.*)&blog_id=(.*)\ HTTP
RewriteRule ^ http://%2.mydomain.com/view_blog/%3? [R,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /live/agent/page.php\?agent_user_name=(.*)&content_id=(.*)\ HTTP
RewriteRule ^ http://%2.mydomain.com/content/%3? [R,L]

htaccess issues and rewrite cond

This is my current script... I need to allow "sub.domain.com/register/USER"
Currently, I have a rerwrite cond for "sub.domain.com" which points right to "sub.domain.com/login"
What am I doing wrong?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteCond %{HTTP_HOST} ^(.+).domain.com$
RewriteRule ^register(.*) https://%1.domain.com/register$1 [L,R=302,QSA]
RewriteCond %{HTTP_HOST} ^(.+).domain.com$
RewriteRule ^$ https://%1.domain.com/login/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
As of update, I have a redirect loop in /register/USER..
Replace your first set of rules with this:
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteCond %{HTTP_HOST} ^(.+).domain.com$
RewriteRule ^register(.*) - [L,R=302,QSA]
EDIT: Maybe a better solution is to rewrite only if the URI isn't /register/user. So get rid of the above rule, and change the one below it to:
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteCond %{HTTP_HOST} ^(.+).domain.com$
RewriteCond %{REQUEST_URI} !^/register/?.*
RewriteRule ^(.*)$ https://%1.domain.com/login/ [L,R=302]

htaccess not working properly

My current htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1
RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?page=$1&subsectie=$2
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?page=$1&subsectie=$2&subid=$3
I've edited the htaccess file. It should only use this htaccess for the main domain and not for subdomains, however subdomains still use this htaccess.
Anyone with more experience than me could point me out in the right direction? Thanks in advance.
Edit:
Here is my current (and working) htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?page=$1&subsectie=$2
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?page=$1&subsectie=$2&subid=$3
Try this:
RewriteEngine On
# first, remove redirect to www by default if no subdomain
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [NC,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&subsectie=$2 [NC,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?page=$1&subsectie=$2&subid=$3 [NC,L]

Resources