htaccess virtual subdomains and parameters - .htaccess

i have an htaccess for creating virtual subdomains which working fine
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt|JPG)$
RewriteRule ^(.*)$ virtialsubdomain.php?subdomain=%1 [L]
With the above i can see for example virtual1.domain.com, virtual2.domain.com etc
I want to extend the htaccess so i can pass 2 parameters for paging and sorting
Example
virtual1.domain.com/p/3 show page 3
virtual1.domain.com/sort/id,asc show results sort by id asc
virtual1.domain.com/sort/id,asc/p/3 the combination of 2 above
For other pages in the www.domain.com i have rules that achieve that
RewriteRule category/([^.]+)/sort/([^.]+)/p/([0-9]+)?$ /file.php?id=$1&sort=$2&p=$3 [L,NC,QSA]
RewriteRule category/([^.]+)/sort/([^.]+)?$ /file.php?id=$1&sort=$2 [L,NC,QSA]
RewriteRule category/([^.]+)/p/([0-9]+)?$ /file.php?id=$1&p=$2 [L,NC,QSA]
But i really dont know how to write the rules when comes the virtual subdomains
Any help appreciated

I always separate things out first. Make all request to subdomains map to the subdomains folder like so:
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^subdomains/
RewriteRule ^(.*)$ /subdomains/$1 [L]
Then in /subdomains/ you create a separate htaccess to do the paging and sorting rules.
RewriteBase /subdomains
RewriteRule ^p/([0-9]+)$ index.php?page=$1 [L,QSA]
RewriteRule ^sort/([^/]+)(/p/([0-9]+))?$ index.php?sort=$1&page=$3 [L,QSA]

Related

htaccess language rediretion

I have a website, www.thesite.com and an alias www.lesite.com
i need a htaccess redirection:
I would like that, when I go to www.thesite.com/fr, it redirects me to www.lesite.com/fr
Likewise, when I go to www.lesite.com/en, I would like it to redirect me to www.thesite.com/en
I have tried different methods but i only succeed to create infinite loops !----
Options +FollowSymlinks
RewriteRule ^fr$ http://dev.mariage.ch/fr/ [L]
RewriteRule ^de$ http://dev.hortzeit.ch/de/ [L]
OR
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch\/de\/
RewriteRule (.*) http://dev.hortzeit.ch/de$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch\/fr\/
RewriteRule (.*) http://dev.mariage.ch/fr$1 [R=301,L]
You can't use path as part of RewriteCond for HTTP_HOST
instead of dev.hortzeit.ch/de you must use just host part dev.hortzeit.ch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch [NC]
RewriteRule ^de(/?.*)$ http://dev.hortzeit.ch/de$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch [NC]
RewriteRule ^fr(/?.*)$ http://dev.mariage.ch/fr$1 [R=301,NC,L]

Swapping domain name with htaccess

I'm trying to migrate a website to another domain and hope to have requests on the old domain redirected to the new domain, no matter what they are, and regardless of how the URL is structured.
The code below in my .htaccess file does this for a domain and any directories/pages but does not cover the many subdomains I have.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !domain1.com$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301]
How can I add to this in order to convert the following (for example):
http://something.domain1.com/directory/page.php?variable=something
http://something.domain2.com/directory/page.php?variable=something
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^(.*)$ http://domain2\.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://%1.domain2\.com/$1 [L,R=301]

Rewrite www.domainnamenumberone.com/wordpress/path/ to domaintwo.com/wordpress/path

I have two domain names for the same website, and need to do a correct rewrite, so that whenever someone accesses the first domain name and all subdirectories, with, or without www. they get redirected to the second domain and subdirectories without www.
I managed to set the redirect for the domain name without subdirectories, but for whatever reason, subdirectories are not getting rewritten.
So when I go to domainnamenumberone.com, or www.domainnamenumberone.com, i get redirected to domaintwo.com – however, when I go to domainnamenumberone.com/wordpress/path or www.domainnamenumberone.com/wordpress/path I remain there, and nothing gets rewritten.
Here's what I placed in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
Would be grateful for your help!
You need to place this rule as very first rule in DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainnumberone\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\s [NC]
RewriteRule ^ http://domaintwo.com/%1 [L,R=302,NE]
Then add this line in each child .htaccess like wordpress/ or wordpress/path/ (wherever .htaccess already exists) below RewriteEngine On line
RewriteOptions Inherit
You can use that:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
But it seems to me that it should also work with yours.....
try a different browser (cache problem)

Dynamic subdomain with htaccess (not redirect)

Currently, I'm having an url system like this by using the htaccess:
- www.domain.com/member/username
- www.domain.com/member/username/contact
- www.domain.com/member/user/album/title-of-the-album/albumID
.. something like that
Here is my htaccess, and it worked perfectly.
RewriteRule ^member/([a-zA-Z0-9_-]+)$ member.php?username=$1
RewriteRule ^member/([a-zA-Z0-9_-]+)/contact$ contact.php?username=$1
RewriteRule ^member/([a-zA-Z0-9_-]+)/album/([a-zA-Z0-9_-]+)/([0-9]+)$ album.php?username=$1&title=$2&album_id=$3
Now I want to setup a dynamic subdomain system for the user, likes "username.domain.com"
So I decided to used the below htaccess:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^(.*)$ www.domain.com/member/%1 [L]
But this redirect the user to their old domain "www.domain.com/member/username" instead of "www.domain.com/member/username"
I want to keep the user stay in "username.domain.com" (no url change in the address bar).
If possible, is there any chance to keep the same structure for other new url, such as when I type:
"username.domain.com/contact" will load the content of "www.domain.com/member/username/contact" (no url change in the address bar)
"username.domain.com/album/title-of-the-album/albumID" will load the content of "www.domain.com/member/username/album/title-of-the-album/albumID" (no url change in the address bar)
Please help !!
Try:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?$ /member.php?username=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?contact$ /contact.php?username=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?album/([a-zA-Z0-9_-]+)/([0-9]+)$ /album.php?username=%1&title=$1&album_id=$2 [L]

Subdomain rewrite rules for file access not to effect main site files

I have set up virtual subdomain. and i want to make htaccess to achive the following flow
For
www.domain.com
it should call the index file but for the subdomain
abc.domain.com
The rewrite rule should be like
RewriteRule www.domain.com/index.php?var=abc
i mean it should (in HTACCESS) pass the subdomain to the index file as an argument
and for the Other file requests like
abc.domain.com/file.php
The subdomain should be rewritten like www.domain.com/file.php?var=abc
I mean the Rewrite rule like
RewriteRule www.domain.com/file.php?var=abc
This would be how you could do them on an individual basis.
RewriteCond %{HTTP_HOST} ^abc\.domain\.com$ [NC]
RewriteRule (.*)$ http://www.domain.com/file.php?var=abc [L]
RewriteCond %{HTTP_HOST} ^def\.domain\.com$ [NC]
RewriteRule (.*)$ http://www.domain.com/file.php?var=def [L]
The solution is something like this
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com/index.php?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]

Resources