Virtual subdomain using htacess, Pages rewrite rules - php

I have make virtual subdomain in my code.like below
RewriteCond %{HTTP_HOST} ^(.*)\.mysitename\.com
RewriteRule ^(.*)$ agent.php?asitename=%1 [L,NC,QSA]
it works fine, but it did not work for pages like
RewriteCond %{HTTP_HOST} ^(.*)\.mysitename\.com
RewriteRule ^(.*)/ag_buy.html ag_buy.php?sitename=%1&page=buy [L,NC,QSA]
it redirect all pages top agent.php, but it should only redirect home page to agent.php, for other pages it should work like ag_buy.html to ag_buy.php
and so on.........
please guide me on htaccess how can i make this possible.

The ^(.*)$ matches everything, if you only want the home page, then change it to ^$:
RewriteCond %{HTTP_HOST} ^(.*)\.mysitename\.com
RewriteRule ^$ agent.php?asitename=%1 [L,NC,QSA]

Try to replace the first rule with following code:
RewriteCond %{HTTP_HOST} ^(.).mysitename.com
RewriteCond %{REQUEST_URI} =/
RewriteRule ^(.)$ agent.php?asitename=%1 [L,NC,QSA]

Related

SEO friendly redirect in .htaccess blocks subfolder

I have this code in my .htaccess to make my URLs SEO friendly and pretty:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?page=$1 [L,QSA]
Now I notice that a subfolder (gallery.mysite.com ) set by my webhotel does no longer work. It gets redirected to index.php and seems to try to load an empty var (page).
Is there a way to force the subdomain not getting redirected?
I have tried this but it doesn't quite get there...
RewriteCond %{HTTP_HOST} ^gallery\.mysite\.com$ [NC]
RewriteRule ^((?!sub1/).*)$ sub1/$1 [L,NC]
RewriteCond %{HTTP_HOST} !^gallery\. [NC]
You can use this
RewriteCond %{HTTP_HOST} !^gallery\.mysite\.com$
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?page=$1 [L,QSA]

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.

Redirect sub domain to sub directory

I want to redirect the the subdomain to subdirectory but not working. Here is my efforts.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^name\.site_url
RewriteRule ^(.*)$ http://site_url/name/$1 [L,R=301]
Try this:
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(.*)$ http://your_domain/sub/$1 [R,L]
Instead sub in RewriteCond and RewriteRule you can place whatever you want
Btw, RewriteCond %{HTTP_HOST} ^sub\.my\.domain$ also works for me. So, check your site url. Or give more info (at least, what happened in apache error.log).

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.

htaccess redirect help please

I previously asked this question and the answer did not seem to work as required - it also required an addition.
Users are provided with unique URL's, ie: exampleurl.com/AQ4ILB9
AQ4ILB9 being the referral code
I would like that URL above (or any referral URL) to display the contents of index.php (htaccess redirect?)
I would like the non-www and www version of exampleurl.com, including example.com/index.php to be redirected to the top level in this format: http://www.exampleurl.com/
I would like for example: $_GET['_url'] to hold the referral id (ie: AQ4ILB9) in index.php
How can I go about the above 3 all using htaccess?
Thanks!
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)$ /index.php?_url=$1 [NC,L,QSA]
The first part is a 301 redirect to add www.
The second part will rewrite what you want, but not for existing files/directories.
Q) 1 & 3:
RewriteRule (.*) index.php?url=$1 [L,P]
Q) 2:
RewriteCond %{HTTP_HOST} ^exampleurl.com
RewriteRule (.*) http://www.exampleurl.com/$1 [R=301,L]
RewriteRule ^/([A-Z0-9]+)$ index.php?_url=$1
Replace example.com with your domain and put this in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule (.*) /index.php?_url=$1 [L]
Should do the trick! You may not need the RewriteEngine On line if it's already there.

Categories