Wildcard subdomain mod-rewrite does not work - php

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]

Related

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 for external domain

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]

.htaccess rewrite not consistent with cookie drop

I have in my .htaccess file the following code:
RewriteRule ^(en|af|fr)/(.*)$ - [co=lang:$1:mysite.info:7200:/] #drop/update cookie with lang
RewriteRule ^(en|af|fr)$ - [co=lang:$1:mysite.info:7200:/] #drop/update cookie with lang
RewriteRule ^(en|af|fr)$ / [R,NC]
RewriteRule ^(en|af|fr)/(.*)$ /$2 [R,NC]
In my php I have this for selecting the language:
<?php echo $_COOKIE['lang']; ?>
<ul>
<li>en-ZA</li>
<li>fr-MU</li>
<li>af-ZA</li>
</ul>
It semi-works, but it's not consistent. While I'm switching between languages it simply stops working, and then starts again if I keep clicking on the different languages.
SECOND PART of question:
How to reduce these lines:
RewriteCond %{http_host} ^www.mysite.info [nc] #if url starts with www.mysite.info
RewriteRule ^(.*)$ http://mysite.info/$1 [r,nc] #rewrite without the www
RewriteCond %{http_host} ^www.(.*).mysite.info [nc] #if url starts with www.(aff).mysite.info
RewriteRule ^(.*)$ http://%1.mysite.info/$1 [r,nc] #rewrite without the www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{http_host} ^(.*)\.mysite.info [nc] #if subdomain is aff
RewriteRule ^(.*)$ - [co=aff:%1:mysite.info:7200:/] #drop/update cookie with aff
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{http_host} ^(.*)\.mysite.info [nc] #if subdomain is aff
RewriteRule ^(.*)$ http://mysite.info/$1 [nc,R] #rewrite without subdomain
Appreciate the help, anubhava!
Not sure you have repeated rules in there. Try this rule to replace all 4 of your rules:
RewriteRule ^(en|af|fr)(/.*)?$ /$2 [co=lang:$1:mysite.info:7200:/,NC,L,R]
UPDATE:
RewriteCond %{http_host} ^([^.]+)\.mysite\.info$ [NC]
RewriteRule ^(.*)$ http://mysite.info/$1 [L,R]
RewriteCond %{http_host} ^www.([^.]+)\.mysite\.info$ [NC]
RewriteRule ^(.*)$ http://%1.mysite.info/$1 [L,R,NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{http_host} ^([^.]+)\.mysite.info [NC]
RewriteRule ^(.*)$ - [co=aff:%1:mysite.info:7200:/]

htaccess subdomain as get with variables

I am trying to make like this.
user.mydomain.com -> mydomain.com/user.php?user=user
user.mydomain.com/content_(.*).xhtml -> mydomain.com/user.php?user=user&content=(.*)
user.mydomain.com/content_(.*).xhtml?get=(.*) ->mydomain.com/user.php?user=user&content=(.*)&get=(.*)
I already tried this
<Directory "/var/www/html">
RewriteEngine On
# host doesn't start with www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^m\. [NC]
# host starts with something else
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain\.com$ [NC]
# rewrite
RewriteRule ^(.*)$ users.php?user=%1
RewriteRule ^users\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^content_(.*).xhtml$ users.php?user=%1&content=$1 [L,QSA]
</Directory>
but it only passing the subdomain not the content parameters value.
Use this code in your DocumentRoot/.htaccess:
RewriteEngine On
RewriteRule ^users\.php$ - [L]
RewriteCond %{HTTP_HOST} !^(?:www|m)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^$ /users.php?user=%1 [L,QSA]
RewriteCond %{HTTP_HOST} !^(?:www|m)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^content_([^.]+)\.xhtml$ /users.php?user=%1&content=$1 [L,QSA,NC]

Using mod-rewrite with multiple pages

Simplified Question
I changed domain.com/folder/client.php?id=1 to 1.domain.com with this .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
Now I want to change domain.com/folder/about.php?id=1 to 1.domain.com/about.
Is it possible ? or is there any other ways to do this?
Thank you :D
Old Question
In my directory, I have 2 pages (client.php, about.php). I've already set a wildcard subdomain to the page directory and changed domain.com/folder/client.php?id=1 to 1.domain.com. This is my .htaccess code :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
Now, I need to add another page. So, in client.php page, there's a button to go to about.php page in the same directory. I tried 1.domain.com/about.php and it's not working.
Do I have to create another line of RewriteCond ? or is there any other ways to do this?
Thank you very much :D
Updated Code without working id
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/about [NC]
RewriteCond %{REQUEST_URI} !about\.php [NC]
RewriteRule . /about.php?id=%1 [L,QSA]
**The code resolved the text in url box. Let's say id=1. Now it's showing 1.domain.com/about.
the php code : `$_GET["id"]` is not specified.**
Thanks to faa for the edit :D
The Full Solution
Refer Using mod-Rewrite with 5 pages (wildcard sub-domain)
You may try this:
Options +FollowSymLinks
RewriteEngine on
## This is the actual rule-set that works as it is according to the OP
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
# Added to exclude folder `/about`
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteRule ^$ /client.php?id=%2 [L,QSA,NC]
## This is the additional rule set
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/about [NC]
RewriteCond %{REQUEST_URI} !about\.php [NC]
RewriteRule . http://domain.com/folder/about.php?id=%1 [L,QSA]
Maps silently
http://N.domain.com/about with or without trailing slash
To:
http://domain.com/folder/about.php?id=N
For permanent and visible redirection, replace [L,QSA] with [L,R=301,QSA]

Categories