Htaccess for external domain - php

RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ load.php?id=%1&q=$1 [NC,QSA,L]
This is my current code
subdomain.domain.com/querystring
The condition is domain.com
Is there a way I can achieve the same thing
but the domain.com can be dynamic, means it could be 123456.co or qwerty1234.xyz/querystring
Yet it able capture
Domain: qwerty1234.xyz
Query String: querystring
How do I make my htaccess to check if domain is not containing domain.com, it will use load.php
but pass the domain & its query string to load.php as ?domain=$domain&query=$querystring
Thanks!
Updated .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ load.php?id=%1&q=$1 [NC,QSA,L]
RewriteCond %{REQUEST_URI} !(\.)?(css|js|png|jpg|gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ load.php?id=%1&q=$1 [NC,QSA,L]

You should be able to add this to your rules. You don't have to do anything special to capture the querystring. Simply keep the QSA flag and any additional query string on the request will also be sent to load.php.
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.)?(css|js|png|jpg|gif|robots\.txt)$ [NC]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ load.php?domain=%1 [NC,QSA,L]

Related

variables in subdomain in htaccess

I have a 2 php pages "download.php" and "windows.php"
the link of the both is
xyn.com/download.php?r=test
xyn.com/windows.php?r=test
Note : the two variable in the two pages are the same
I want to make the link of the pages like this
1 - download."variable".domain.com -> download.php?r=test
2 - download."variable".domain.com/windows -> windows.php?r=test
the code I am using is :
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^download\.(.+?)\.domain\.com$
RewriteRule ^(.*)$ download.php?r=%1 [L]
RewriteCond %{HTTP_HOST} ^download\.(.+?)\.domain\.com/windows$
RewriteRule ^(.*)$ windows.php?r=%1 [L]
the first link is working fine :
download.variable.domain.com
but the second link is taking me to the first link :
download.variable.domain.com/windows
so any idea ?
RewriteCond %{THE_REQUEST} \h/\h
RewriteCond %{REQUEST_URI} !^/download\.php
RewriteCond %{HTTP_HOST} ^download\.([^\.]+)\.domain\.com$
RewriteRule ^ download.php?r=%1 [L]
RewriteCond %{THE_REQUEST} \h/windows\h
RewriteCond %{REQUEST_URI} !^/windows\.php
RewriteCond %{HTTP_HOST} ^download\.([^\.]+)\.domain\.com$
RewriteRule ^ windows.php?r=%1 [L]
all these subdomains should point to one document root
Both conditions are on same HTTP_HOST, just add the REQUEST_URI as a condition.
Try this:
RewriteCond %{REQUEST_URI} !^/windows
RewriteCond %{HTTP_HOST} ^download\.(.+?)\.YourDomain\.net$
RewriteRule ^ http://YourDomain.net/download.php?r=%1 [L]
RewriteCond %{REQUEST_URI} ^/windows
RewriteCond %{HTTP_HOST} ^download\.(.+?)\.YourDomain\.net$
RewriteRule ^ http://YourDomain.net/windows.php?r=%1 [L]
http://download.babjhons.YourDomain.net/windows
=>
http://YourDomain.net/windows.php?r=babjhons

htaccess rewrite only if request uri is empty

My current .htaccess is as follow:
##FOR DUAL LEVEL TIER SUBDOMAIN
#SUBDOMAIN REWRITE for COUNTRY.CATEGORY.uqloo.com
RewriteCond %{HTTP_HOST} !www.mydomain.com$ [NC] # Presuming you don't want to do www
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.mydomain\.com [NC] # Catch subdomain
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteRule ^(.*)$ index.php?sub1=%1&sub2=%2 [QSA,L]
##FOR SINGLE LEVEL TIER SUBDOMAIN
RewriteCond %{HTTP_HOST} !www.mydomain.com$ [NC] # Presuming you don't want to do www
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com [NC] # Catch subdomain
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteCond %{REQUEST_URI} !^cat/([A-Za-z0-9-]+)?$ [NC] # Don't rewrite if we already have
RewriteRule ^(.*)$ index.php?sub1=%1 [QSA,L]
I'm trying to do single and dual level subdomain redirect only while keeping all my other rewrites for the URI parts relevant. I want to keep the parameters for subdomain relevant as well for the URI rewrites.
An example will be the following rule keeps getting redirected back to index.php when it should go to pages/details.php with parameters sub1 and sub2 available if the subdomain is present.
RewriteRule ^([0-9-]+)?/([A-Za-z0-9-]+)?$
pages/details.php?adid=$1&alias=$2 [NC,L]
You can use these rules:
RewriteEngine On
# pages URL handlers
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^([0-9-]+)/([a-z0-9-]+)/?$ pages/details.php?sub1=%1&adid=$1&alias=$2 [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^([0-9-]+)/([a-z0-9-]+)/?$ pages/details.php?sub1=%1&sub2=%2&adid=$1&alias=$2 [NC,L,QSA]
# subdomain rewrites
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteCond %{REQUEST_URI} !^/cat/([A-Za-z0-9-]+)?$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^ index.php?sub1=%1 [QSA,L]
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^ index.php?sub1=%1&sub2=%2 [QSA,L]

htaccess stop processing if given directory

I have an instalation on my htdocs folder that needs rewriterules to be processed like this:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteCond %{REQUEST_URI} !^/admin?
RewriteCond %{REQUEST_URI} !^/payment?
RewriteRule ^(.*)$ %1/$1 [QSA]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^admin/(.*)?$ BACKEND-PHP/$1?domain=%1 [QSA]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^payment/(.*)?$ PAYPAL-API/$1?domain=%1 [QSA]
But now, to test joomla i need add some rule in the htaccess file that stop processing rules if the given directory is "localhost/joomla" in order to joomla work properly.
in pseudocode will be like this:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
**RewriteCond %{HTTP_HOST} ^(localhost)$
**RewriteRule ^/joomla$ [END]
# (if the requested file is at joomla directory
# htaccess will stop processing)
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteCond %{REQUEST_URI} !^/admin?
RewriteCond %{REQUEST_URI} !^/payment?
RewriteRule ^(.*)$ %1/$1 [QSA]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^admin/(.*)?$ BACKEND-PHP/$1?domain=%1 [QSA]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^payment/(.*)?$ PAYPAL-API/$1?domain=%1 [QSA]
You were close. Just modify your rules to
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^joomla(/.*)?$ - [END]
The - says we do no processing on this URL. The [End] flag prevents all the rules below from firing.
You can use this in the subdirectory:
RewriteEngine off

htaccess remove www and redirect to accessed url

I am using codeigniter framework i need to force to remove www from the url so I am using this code
RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,QSA,NC,L]
This code is forcing removal of www. but the problem is when a user access a link with www
eg:www.mydomain.com/framework/article/sometestarticle368/
It is redirecting to
www.mydomain.com/framework/
How can i fix this ?
Change the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L,QSA]
Otherwise your 2nd rules runs first and change the URI to /framework/... before the www removal rule..
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Thanks

Wildcard subdomain mod-rewrite does not work

I have a wildcard subdomain *.domain.com assigned to public_html/.
I want to do like this:
For example, /folder1/index.php is based on state name(?state=statename).
For the /folder1/folder2/index.php, it will be based on unique name(?name=uniquename).
So, www.domain.com/folder1/index.php?state=statename will be statename.domain.com
and www.domain.com/folder1/folder2/index.php?name=uniquename will be uniquename.domain.com
This is my code
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/index\.php\?state=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/folder2/index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
The problem is it redirect back to public_html directory. Is there any problem with the code?
Old Code
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^$ /index [L]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^(index)/?$ /$1.php?name=%2 [L,NC,QSA]
Code explanation : Whenever user enter uniquename.domain.com, it will automatically go to www.domain.com/index.php?name=uniquename and the uniquename.domain.com in url bar would not change.
The differences for the new problem is there are different state directory and the domain would be state1.uniquename.domain.com. The 'state1.uniquename.domain.com' in the url bar should not change too.
Based on your comments. Make sure DOCUMENT_ROOT for www.domain.com, state1.domain.com, state2.domain.com is public_html
Try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/index\.php\?state=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder1/folder2/index\.php\?name=([^\s&]+) [NC]
RewriteRule ^ http://%1.domain.com/? [R=301,L]
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.domain\.com$ [NC]
RewriteRule ^ /state/%1/client/index.php?name=%2&page=%{REQUEST_URI} [L,NC]

Categories