I want to rewrite subdomains and folders to params - .htaccess

I like to rewrite my subdomain site (wildcard) and when exists folders to params for the index.php file.
So
www.domain.com
www.domain.com/js/file.js
www.domain.com/css/file.css
www.domain.com/images/file.jpg
should be preserved!
And these should change to parameters for index.php
sub.domain.com/
to
www.domain.com/index.php?key1=sub
sub.domain.com/2015
to
www.domain.com/index.php?key1=sub&key2=2015
sub.domain.com/2015/12
to
www.domain.com/index.php?key1=sub&key2=2015&key3=12
Is this possible?
What I have sofar is
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteRule ^/?(.*)$ $1?page=%1 [QSA,L,NE]
Thank you very much!
So I rewritten some of #anubhava his htaccess to this
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.gebruikmaar\.nl [NC]
RewriteRule ^/?(.*)$ $1?page=%1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.gebruikmaar\.nl [NC]
RewriteRule ^(.*)([^/][0-9]*)(/?)$ $1?page=%1&key2=$1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.gebruikmaar\.nl [NC]
RewriteRule ^(.*)([^/][0-9]*)/([^/][0-9]*)(/?)$ $1?page=%1&key2=$1&key3=$2 [QSA,L]
And it almost works!
This url:
http://agenda.gebruikmaar.nl
Becomes
http://www.gebruikmaar.nl/index.php?page=agenda
BUT :-(
This url:
http://agenda.gebruikmaar.nl/2015
Becomes:
http://www.gebruikmaar.nl/index.php?page=agenda&key1=201
So where's the last character (5) at?

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteRule ^/?$ index.php?key1=%1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteRule ^([^/]+)/?$ index.php?key1=%1&key2=$1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?key1=%1&key2=$1&key3=$2 [QSA,L]

Related

Can .htaccess rules be cleaned-up?

I am helping with a project and noticed the following rules in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-projectdomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-projectdomain.com$
RewriteRule (.*)$ https://www.new-projectdomain.com/$1 [R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I'm not an expert on this but it looks somewhat redundant, I just can't put my finger on it.Can it be simplified?
You can use a single rule like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-projectdomain\.com$ [NC]
RewriteRule ^ https://www.new-projectdomain.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

how to redirect www.xyz.com/webmail to mail.xyz.com through .htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^webmail$ "http\:\/\/mail\.domain\.com\/" [R=301,L]
But Above code in .htaccess is not working however given below code is working
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^folderName\/$ "http\:\/\/www\.domain\.net\/" [R=301,L]
Try using this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.xyz.com
RewriteRule ^webmail/(.*)$ http://mail.xyz.com/$1 [L,R=301]
EDIT:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?xyz\.com$
RewriteRule ^webmail/(.*) http://mail.xyz.com/$1 [R=301,L]

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]

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]

Subdomains rewrite .htaccess

I have these rules, everything work good.
I just need to add this rule:
from it.domain.org/index.php?city=newyork to it.domain.org/product-usa-newyork
The only thing that change is "newyork". It can be also: lasvegas etc..
Try inserting these 2 rules before your existing rules:
#Rewrite from http://it.domain.org/index.php?city=dc to http://it.domain.org/product-usa-dc
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^\s&]+) [NC]
RewriteRule ^ /product-usa-%1? [R=302,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product-usa-([^/]+)/?$ /index.php?city=$1 [L,QSA,NC]
Put it at the start
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.org$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^\s&]+) [NC]
RewriteRule ^ /product-usa-%1 [R=302,L]

Resources