.htaccess rewrite not consistent with cookie drop - php

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:/]

Related

redirecting www. to https

My website url looks like www.[site-name].com
I want to redirect to https://www.[site-name].com on .htaccess with following code.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301, L]
I copy the file to ftp at this path: httpdocs/.htaccess
But it does not redirect to [site-name].com when I try to load www.[site-name].com. How can I solve this?
Try this:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,QSA]
If you want to redirect non-www url to www url , for both http and https use this ;
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
If you want to redirect www to non-www url use this ;
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
or without using domainname ;
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]

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]

RewriteCond %{HTTP_HOST} not responding

I want to rewrite urls based on subdomain names. But when go to http://test2.domain.com/test/ it goes to http://test1.domain.com/test/ . When I was testing I found that when I removed the first RewriteRule line (of the test1.domain.com part) (RewriteRule ^test/&(.*)$ index.php?menuID=1&$1 [L]) it was working. What is going wrong?
###start_test1.domain.com###
RewriteCond %{HTTP_HOST} ^(test1.)?domain.com [NC]
RewriteCond %{REQUEST_URI} ^/
RewriteRule ^test/&(.*)$ index.php?menuID=1&$1 [L]
RewriteRule ^test/ index.php?menuID=1[L]
###end_test1.domain.com###
###start_test2.domain.com###
RewriteCond %{HTTP_HOST} ^(test2.)?domain.com [NC]
RewriteCond %{REQUEST_URI} ^/
RewriteRule ^test/&(.*)$ index.php?menuID=2&$1 [L]
RewriteRule ^test/ index.php?menuID=2[L]
###end_test2.domain.com###
RewriteCond is only applicable to vey next RewriteRule. Also your regex ^(test2.)?domain.com is not looking correct.
Have your rules like this:
RewriteEngine On
###start_test1.domain.com###
RewriteCond %{HTTP_HOST} ^test1\.domain\.com$ [NC]
RewriteRule ^test/&(.*)$ index.php?menuID=1&$1 [L,B]
RewriteCond %{HTTP_HOST} ^test1\.domain\.com$ [NC]
RewriteRule ^test/ index.php?menuID=1[L]
###end_test1.domain.com###
###start_test2.domain.com###
RewriteCond %{HTTP_HOST} ^test2\.domain\.com$ [NC]
RewriteRule ^test/&(.*)$ index.php?menuID=2&$1 [L,B]
RewriteCond %{HTTP_HOST} ^test2\.domain\.com$ [NC]
RewriteRule ^test/ index.php?menuID=2[L]
###end_test2.domain.com###

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