htaccess rewrite only if request uri is empty - php

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]

Related

.htaccess: Redirect domain to https not working if also changing the root path

I have the following domains:
example1.com
example2.com
example3.com
The domains point to /public_html/. There are three things I want to do in /public_html/.htaccess:
Redirect (with all parameters and paths) the domains example2.com and example3.com to the domain example1.com.
example1.com itself should get shown (if not, then redirect) always with https and www, means: https://www.example1.com
The custom root path for the domains is /public_html/. I want to change this to /public_html/example_path/.
I have the following in /public_html/.htaccess:
RewriteCond %{HTTP_HOST} ^example2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example2.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example3.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example3.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example1.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example1.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example1.com$
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]
This is nearly working as expected. But when opening http://www.example1.com there is no redirection to https://www.example1.com. This is only working when removing the last four code lines, that should change the root path.
For all other domains it's working.
Why isn't it woking for http://www.example1.com? And why is it only working when not changing the root path?
Add this rule to yours at the top of the .htaccess file
# http <> https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
Or check these rewrites instead of your rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example1\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]
</IfModule>
For specific domains (www/non-www example-other.co, www/non-www example3.com, www/non-www example2.com, example1.com)
<IfModule mod_rewrite.c>
RewriteEngine On
# http www/non-www example-other.com/any to https://www.example-other.com/any
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example-other\.com [NC]
RewriteRule (.*) https://www.example-other.com/$1 [R=301,L]
# https non-www example-other.com/any to https://www.example-other.com/any
RewriteCond %{HTTP_HOST} ^example-other\.com [NC]
RewriteRule (.*) https://www.example-other.com/$1 [R=301,L]
# http www/non-www example1.com/any to https://www.example1.com/any
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# http/https www/non-www example2.com/any to https://www.example1.com/any
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# http/https www/non-www example3.com/any to https://www.example1.com/any
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# https non-www example1.com/any to https://www.example1.com/any
RewriteCond %{HTTP_HOST} ^example1\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# Rewrite /any to /example_path/any
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]
</IfModule>

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 rewrites for subdomain

I want to make all URLs of the site belong to the root domain.com even the assets, and only categories in sub domains
example
domain.com/
domain.com/join.php
domain.com/admin/
games.domain.com/
games.domain.com/arcade/
games.domain.com/arcade/10.html
each of these url take from ===
domain.com/ === index.php
domain.com/join.php === join.php
domain.com/admin/ === /admin/
games.domain.com/ === main_categories.php?slug=games
games.domain.com/arcade/ === sub_categories.php?slug=games&cat=arcade
games.domain.com/arcade/10.html === view.php?slug=games&cat=arcade&id=10
I have this code but its not working
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain.com
RewriteRule .* - [S=3]
RewriteRule ^(.*)$ main_categories.php?prefix=%1
RewriteRule ^([A-Za-z0-9-]+)/?((index|news|photos|videos|articles)\.html)?$ sub_categories.php?prefix=$1&type=$3 [QSA,L]
RewriteRule ^([A-Za-z0-9-]+)/([0-9-]+)/?(.*)?.html$ view.php?prefix=$1&id=$2&title=$3 [QSA,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+).domain.ru$
RewriteRule ^(.+)/(.+)\.html$ /view.php?slug=%1&cat=$1&id=$2 [L]
RewriteCond %{HTTP_HOST} ^(.+).domain.ru$
RewriteRule ^(.+)/$ /sub_categories.php?slug=%1&cat=$1 [L]
RewriteCond %{HTTP_HOST} ^(.+).domain.ru$
RewriteRule ^$ /main_categories.php?slug=%1 [L]
If your Apache configuration does not do this for you, add this
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^$ /index.php
If I understand you at all. First, you will need to update your Apache config or Virtual Host ServerAlias to include any subdomains *.example.com you want Apache to respond to. You will probably need to make a wildcard subdomain DNS entry so it knows where to send the request when someone types in games.example.com.
Then you will need to make a rule to exclude www and non-www by using these rules below.
#games.domain.com/ === main_categories.php?slug=games
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.example\.com$ [NC]
RewriteRule ^ main_categories.php?slug=%1 [L,QSA]
#games.domain.com/arcade/ === sub_categories.php?slug=games&cat=arcade
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.example\.com$ [NC]
RewriteRule ^([^/]+)/?$ sub_categories.php?slug=%1&cat=$1 [L,QSA]
#games.domain.com/arcade/10.html === view.php?slug=games&cat=arcade&id=10
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.example\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)\.html$ view.php?slug=%1&cat=$1&id=$2 [L,QSA]

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

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