.htaccess - conditionaly redirect subfolders - .htaccess

I would like to implement followin in .htaccess file:
I have "dictionary", which tells me, which subfolder should be redirected where, i.e.
domain1.com/a -> domain2.com/x
domain1.com/b -> domain2.com/y
and everything else should be redirected to
domain2.com/z
That should be simple enough, but I can not figure it out.
My code:
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com/a$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://domain2.com/x [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com/b$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://domain2.com/y [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://domain2.com/z [L]

Variable %{HTTP_HOST} only matches domain not request uri.
You can use these rules:
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^a/?$ %{ENV:proto}://domain2.com/x [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^b/?$ %{ENV:proto}://domain2.com/y [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^ %{ENV:proto}://domain2.com/z [L,NC,R=301]

Related

merging sites and redirecting old urls

We have merged 2 sites and have to organise redirects from the domain that is now not in use.
The domain aquarestaurantblackheath.co.uk redirects fine, but not the old urls from that domain.
Any suggestions. see my code...
Working
RewriteCond %{HTTP_HOST} ^aquarestaurantblackheath\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.aquarestaurantblackheath\.co\.uk$
RewriteRule ^/?$ "http\:\/\/www\.aquabarandgrill\.co\.uk\/section\/3\/1\/restaurant\-blackheath" [R=301,L]
Not working
RewriteCond %{HTTP_HOST} ^aquarestaurantblackheath\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.aquarestaurantblackheath\.co\.uk$
RewriteRule ^section\/8\/1\/location$ "http\:\/\/www\.aquabarandgrill\.co\.uk\/section\/10\/1\/restaurant" [R=301,L]
RewriteCond %{HTTP_HOST} ^aquarestaurantblackheath\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.aquarestaurantblackheath\.co\.uk$
RewriteRule ^section\/6\/1\/wines$ "http\:\/\/www\.aquabarandgrill\.co\.uk\/section\/8\/1\/food\-wine\-blackheath" [R=301,L]
RewriteCond %{HTTP_HOST} ^aquarestaurantblackheath\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.aquarestaurantblackheath\.co\.uk$
RewriteRule ^section\/7\/1\/reservations$ "http\:\/\/www\.aquabarandgrill\.co\.uk\/section\/3\/1\/restaurant\-blackheath" [R=301,L]
The entire htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^aquabarandgrill.co.uk [NC]
RewriteRule ^(.*)$ http://www.aquabarandgrill.co.uk/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.aquabarandgrill.co.uk/ [R=301,L]
RewriteRule ^index\.html$ http://www.aquabarandgrill.co.uk/ [R=301,L]
RewriteRule ^index\.htm$ http://www.aquabarandgrill.co.uk/ [R=301,L]
RewriteEngine on
Options +SymlinksIfOwnerMatch +MultiViews
RewriteRule ^(.*).php/(.*) $1.php?$2
## Bromley old site REDIRECTS (PERMANANT 301) ##
RewriteRule ^Home.php?id=Menus/?$ http://www.aquabarandgrill.co.uk/section/2/1/restaurant-bromley/ [L,R=301,NC]
#
## Deafult character encoding UTF-8 / ISO-8859-1
AddDefaultCharset ISO-8859-1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^aquarestaurantblackheath\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.aquarestaurantblackheath\.co\.uk$
RewriteRule ^/?$ "http\:\/\/www\.aquabarandgrill\.co\.uk\/section\/3\/1\/restaurant\-blackheath" [R=301,L]
RewriteCond %{HTTP_HOST} ^aquabrasseriecroydon\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.aquabrasseriecroydon\.com$
RewriteRule ^/?$ "http\:\/\/www\.aquabarandgrill\.co\.uk\/section\/4\/1\/restaurant\-croydon" [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?aquarestaurantblackheath\.co\.uk [NC]
RewriteRule ^(/?|section/7/1/reservations)$ http://www.aquabarandgrill.co.uk/section/3/1/restaurant-blackheath [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?aquarestaurantblackheath\.co\.uk [NC]
RewriteRule ^section\/8\/1\/location$ http://www.aquabarandgrill.co.uk/section/10/1/restaurant [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?aquarestaurantblackheath\.co\.uk [NC]
RewriteRule ^section\/6\/1\/wines$ http://www.aquabarandgrill.co.uk/section/8/1/food-wine-blackheath [R=301,L]
Try the following rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?aquarestaurantblackheath\.co\.uk [NC]
RewriteRule ^(/?|section/7/1/reservations)$ http://www.aquabarandgrill.co.uk/section/3/1/restaurant-blackheath [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?aquarestaurantblackheath\.co\.uk [NC]
RewriteRule ^section/8/1/location$ http://www.aquabarandgrill.co.uk/section/10/1/restaurant [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?aquarestaurantblackheath\.co\.uk [NC]
RewriteRule ^section/6/1/wines$ http://www.aquabarandgrill.co.uk/section/8/1/food-wine-blackheath [R=301,L]
EDIT
After seeing the whole htaccess file, remove everything in there, and replace the whole file with the following piece of code:
AddDefaultCharset ISO-8859-1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?aquarestaurantblackheath\.co\.uk [NC]
RewriteRule ^(/?|section/7/1/reservations)$ http://www.aquabarandgrill.co.uk/section/3/1/restaurant-blackheath [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?aquarestaurantblackheath\.co\.uk [NC]
RewriteRule ^section/8/1/location$ http://www.aquabarandgrill.co.uk/section/10/1/restaurant [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?aquarestaurantblackheath\.co\.uk [NC]
RewriteRule ^section/6/1/wines$ http://www.aquabarandgrill.co.uk/section/8/1/food-wine-blackheath [R=301,L]
RewriteRule ^(.*)\.php/(.*) $1.php?$2 [L]

ModRewrite Redirect Loop

Basically I have this code on my htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(login.php|signup.php)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteRule !^(login.php|signup.php)$ http://%{HTTP_HOST}/$1 [R=301,L]
It was supposed to send the user to a SSL Connection. But it keeps having a redirect loop. What Am I doing wrong?
You can't use back-references with negative patterns.
RewriteRule !^login\.php$|^signup\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
or this
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !login\.php$
RewriteCond %{REQUEST_URI} !signup\.php$
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI} [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 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]

Rewrite rule for dynamic subdomain redirecting each file with subdomain arguments

I want to redirect all the calls to files in a subdomain with an extra variable. like if a user access www.domain.com/news.php and in the subdomain he accesses the same page then it should add an extra argument to the url like xyz.domain.com/news.php should be re written like it calls the file news.php?subdomain=xyz. Also i have other rules for just simple domain.
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^$ /index.php?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]
The solution pointed to by adaptr at: https://serverfault.com/a/409171/128746 - would solve this problem very well.

Resources