RewriteCond %{HTTP_HOST} not responding - php

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###

Related

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]

Multiple RewriteRules for single RewriteCond

i have this code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com:8080
RewriteRule ^ http://www.domain.com:8080%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/([0-9-]+)/?(.*)?\.html$ view.php?prefix=%1&cat=$1&id=$2&title=$3 [L,QSA]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/?((index|news|photos|videos|articles)\.html)?$ categories.php?prefix=%1&cat=$1 [L,QSA]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule ^(index\.html)/?$ category_index.php?prefix=%1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com:8080 [NC]
RewriteRule .? - [S=3]
RewriteRule ^(tube|login|register|facebook|logout)\.html$ $1.php [L,QSA]
RewriteRule ^page/([A-Za-z0-9-]+).html$ page.php?prefix=$1
RewriteRule ^sitemap\.xml sitemap.php [QSA,L]
</IfModule>
i want login.html and all sisters open only on www.domain.com/login.html and give 404 error if opened on games.domain.com/login.html
and also RewriteRule .? - [S=3] not work for me, i'm tried to do
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule .? - [S=3]
RewriteRule ^([A-Za-z0-9-]+)/([0-9-]+)/?(.*)?\.html$ view.php?prefix=%1&cat=$1&id=$2&title=$3 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/?((index|news|photos|videos|articles)\.html)?$ categories.php?prefix=%1&cat=$1 [L,QSA]
RewriteRule ^(index\.html)/?$ category_index.php?prefix=%1 [L,QSA]
its not working here and here
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com:8080 [NC]
RewriteRule .? - [S=3]
RewriteRule ^(tube|login|register|facebook|logout)\.html$ $1.php [L,QSA]
This condition is wrong:
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080 [NC]
As you can only match host name using HTTP_HOST variable without port. To make it work use this compound condition:
RewriteCond %{HTTP_HOST}:%{SERVER_PORT} !^(www\.)?domain\.com:8080$ [NC]

How to set up Apache mod_rewrite redirect rules?

I am not quite familiar with Apache settings. I need to make website loading sub-directory content except one page.
Currently got a website and need to make all calls to http://www.domain.com & http://domain.com load contents from http://www.domain.com/subfolder (but looks like http://www.domain.com)
Only except the http://www.domain.com/checkout page, this one page should redirect to https://www.domain.com/checkout for secure checkout
The current mod_rewrite shown as below:
RewriteEngine on
RewriteRule ^$ domain/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/domain%{REQUEST_URI} -f
RewriteRule .* domain/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* domain/index.php?q=$0 [QSA]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://domain.com.au/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://domain.com.au$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.domain.com.au [R,NC]
Open the file named .htaccess in the root of your webserver and add following lines of code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]
rewrite for your complete .htaccess-file (check if this works, then I'll delete the previous code):
RewriteRule ^$ subfolder/index.php [QSA,L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.yourdomain.com [NC]
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^subfolder/(.*) /subfolder/index.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]

Rewriting is not working but gives an internal error in .htaccess

RewriteEngine On
RewriteRule ^http://example.lk/example1/ln1/([^/]*)$ http://example.lk/example1?ln1=$1 [L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ "http\:\/\/example\.lk\/ln\/1" [R=301,L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^contactus$ "http\:\/\/example\.lk\/example3\/cm\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.lk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.lk$
RewriteRule ^example2$ "http\:\/\/example\.lk\/example2\/a\/\$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.lk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.lk$
RewriteRule ^example4$ "http\:\/\/example\.lk\/example4\?\/vi\/\$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^example6$ "http\:\/\/example\.lk\/example6\/id\/\$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^example1$ "http\:\/\/example\.lk\/example1\/ln2\/\$1" [R=301,L]
As pointed out, there a number of errors, please read the manual.
You don't need to double quote or escape the redirect substitutions.
You don't need to match the ${HTTP_HOST} if you don't care what it is.
You can't have a $1 in the substitution without a matching group in the pattern.
I've tried to fix some of the problems... but you need to provide more information on what specifically doesn't work and what you're trying to achieve.
RewriteEngine On
RewriteRule ^example1/ln1/([^/]*)$ http://example.lk/example1?ln1=$1 [L]
# RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ http://example.lk/ln/1 [R=301,L]
# RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^contactus$ http://example.lk/example3/cm/ [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.lk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.lk$
RewriteRule ^example2$ http://example.lk/example2/a/ [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.lk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.lk$
RewriteRule ^example4$ http://example.lk/example4/vi/ [R=301,L]
#RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^example6$ http://example.lk/example6/id/ [R=301,L]
#RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^example1$ http://example.lk/example1/ln2/ [R=301,L]

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