Apache .htaccess rewrite multiple condition with parameters by subdomain - php

I currently have this in my .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ index.php?id=%1 [L,NC,QSA]
All my subdomain regardless is abc.domain.com or def.domain.com will be pointing towards index.php?id=abc and index.php?id=def respectively.
This is currently working perfectly fine on my website however I have other file path like domain.com/faq.php, Hence when user access abc.domain.com/faq.php , this will be pointing to faq.php?id=abc as well.
So my question is how to capture the file path parameter and rewrite with the parameter being pass?
Thank you for the help and please let me know if you require any other information. I'm currently new to rewrite mod hence, this might be a duplicate question.

You can try this rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*\.php)$ $1?id=%1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ index.php?id=%1 [L,NC,QSA]

Related

Subdomain URL Redirect dynamically. .htaccess

I want redirect url using .htaccess
like.
abc.website.com to website.com/folder/abc
http://abc.website.com to http://www.website.com/folder/abc
using .htaccess from PHP server
Use below rule,
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www|webmail|help|whm|root)
RewriteCond %{HTTP_HOST} ^(.+?)\.website.com
RewriteRule ^ http://www.website.com/folder/%1 [R=301,L]
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc.website.com
RewriteRule ^(.*)$ http://website.com/folder/abc/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.abc.website\.com
RewriteRule ^(.*)$ http://website.com/folder/%1/$1 [L,NC,QSA]
I hope it will work for you.

.htaccess rewrite all, except one page

I am trying to solve a problem with my .htaccess file.
I would like to use 301 redirect from my "old" doman to the "new" one, except some certain pages. Like "PageA" and "PageB".
The code thats working and iam using for redirect all is this:
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old.com [NC]
RewriteRule ^(.*)$ http://www.new.com/$1 [L,R=301,NC]
"PageA", and "PageB" has to be redirected to the "new" domain too, but to a certain address. Like:
old.com/pageA ---> new.com/something/pageA
old.com/pageB ---> new.com/something2/pageB
What code should i add to .htaccess, to add some exceptions with certain addresses ?
Try below rule,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(pageA|pageB)$
RewriteRule ^ http://www.new.com/test/%1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com [NC]
RewriteRule ^ http://www.new.com/$1 [L,R=301,NC]

redirect folder subdomain to subdomain.domain.com in htaccess

I have a domain name domain.com and some folders inside it. When i am accessing the folders inside this domain i can access the folder like this.
http://www.domain.com/folder
I want to convert this url with htaccess to
http://www.folder.domain.com
I would like to have similar urls for all folders so its a wildcard entry.
Please tell me how can it be done with htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ http://subdomains.domain.com/$1 [L,NC,QSA]
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com [NC]
RewriteRule ^([^/]+)(/.*)?$ http://www.$1.domain.com$2 [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
For other you have to set dynamic name and pass as parameter, for simple blog you can take help of this question.

Change to www if not a subdomain

Plot: I actually like www URL's but not when used with subdomain like www.whatever.example.com . First i was using two domains on my hosting account so I added a htaccess rewrite like below code.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Motive: Now after sometime I realised that I need a subdomain also pointed to my hosting account. But I don't want a ugly subdomain like www.whatever.example.com but at the sametime I want to redirect example.com users to www.example.com.
I am unable to figure out how to do that. Anyone here.
You can use this rule to only target your main domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Or else, to target any sub-URL down to 3 levels 'deep':
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^[^.]+\.[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Try this using .htaccess,
Ref
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

.htaccess Dynamic sub domains

I want to have dynamic subdomains, such as:
(.*).mydomain.com
that I can retreive with $_GET['subdomain']. I have added an wildcard DNS record (* A xx.xx.xx.xx), and now for every subdomain I go to (e.g. test.mydomain.com) all I get is:
Apache is functioning normally
I have tried this code in .htaccess, but nothing seems to help:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$
RewriteRule ^(.*)$ http://www.mydomain.com/?subdomain=%1 [L]
Thanks in advance!
Are you looking for something more like this?
RewriteCond %{QUERY_STRING} !subdomain=
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /$1?subdomain=%1 [L,QSA]

Categories