variables in subdomain in htaccess - php

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

Related

htaccess rewrite based on domain

my project has multiple domains and I would like to redirect 100 pages for 1 of these domain.
Now I found this post on how to set up conditions for 1 domain.
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^(.*)$ index.php?lang=it [NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.site2\.com [NC]
RewriteRule ^(.*)$ index.php?lang=en [NC,QSA]
My question would be, how can I set this up for 100 pages?
Do I have to duplicate the condition for each link?
What I want to do is this
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^some https://www.mypage.de/shop/some [R=301, L]
RewriteRule ^page https://www.mypage.de/shop/page [R=301, L]
RewriteRule ^settings https://www.mypage.de/shop/settings [R=301, L]
There are so many scenarios to do in this case , for example , you could redirect every page except specific page like this :
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/(page1|page2|page3|whatever)
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [R=301, L]
But , if the number of page who should be excluded is large , you could create directory , for example , new then move all files that have to be redirected at it , then make this rule
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/new%{REQUEST_URI} -f
RewriteRule ^(.*)$ /new/$1 [L]
Then you could add this to redirect any URI contains new to that new location :
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} ^/new/
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [R=301, L]
So code should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/new%{REQUEST_URI} -f
RewriteRule ^(.*)$ /new/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com [NC]
RewriteCond %{REQUEST_URI} ^/new/
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [R=301, L]
Assuming your page names are same after redirect as you've shown in your question, you can use a single rule like this as your top most rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.site1\.com$ [NC]
RewriteRule .+ https://www.mypage.de/shop/$0 [R=301,NE,L]
Make sure to use a new browser for your testing.
Probably Skip flag will be the best aproach.
It looks like this:
RewriteCond %{HTTP_HOST} !^www\.site1\.com [NC]
RewriteRule .? - [S=100]
RewriteRule ^some https://www.mypage.de/shop/some [R=301, L]
RewriteRule ^page https://www.mypage.de/shop/page [R=301, L]
RewriteRule ^settings https://www.mypage.de/shop/settings [R=301, L]
.....
RewriteRule ^rule_for_100nd_page https://www.mypage.de/shop/settings [R=301, L]
How this works.
Test HOST if NOT skip 100 (S=100) the next RewriteRules if YES RewriteRule .? - [S=100] is ignored
Write your 100 rules for www.site1.com source domain and go on.
RewriteRule ^(.*)$ https://www.mypage.de/shop/$1 [QSA]
This will redirect every single page.
If you only want 100 specific pages you must add one by one. Like other answers were provided.
So after testing some of the suggestions I ended up having to add the condition to each rule in order to make it work.
So as far as I can tell you can't set conditions for more than one rule if the targets are to specific.
So my solution for this issue looks like this.
Thanks for all the suggestions.
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^some https://www.site1.de/shop/some [R=301, L]
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^page https://www.site1.de/random/page [R=301, L]
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^settings https://www.site1.de/whatever/settings [R=301, 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]

Cannot access subpage url of subdomain

I did a rewrite rule for wildcard subdomain access to allow me to access any countries of my list page as follows:
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.net$
RewriteCond %{HTTP_HOST} !^www\.mydomain\.net$
RewriteCond %{REQUEST_URI} !^.*\.(jpe?g|png|gif|bmp)$ [NC]
RewriteRule (.*) pages/list.php?country=%1 [L]
This is working fine.
Now. I do a rewrite rule to access another page on the domain but i keep getting referred back to the list page.
RewriteRule ^ad/([A-Za-z0-9-]+)?$ pages/details.php?alias=$1 [NC,L]
My end result is to access eg: http://us.mydomain.net/ad/alias but it keeps referring me back to the list page.
UPDATED REWRITE RULES(AS OF 14 MAY)
RewriteEngine on
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
RewriteRule ^cat/([A-Za-z0-9-]+)/?$ ?category=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(.+)\.ibizportal\.net$
RewriteCond %{HTTP_HOST} !^www\.ibizportal\.net$
RewriteCond %{REQUEST_URI} !^.*\.(jpe?g|png|gif|bmp)$ [NC]
RewriteRule (.*) pages/list.php?country=%1 [L]
Change first rule to:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^cat/([A-Za-z0-9-]+)/?$ ?category=$1 [L,QSA,NC]
RewriteCond %{HTTP_HOST} ^((?!www).+)\.mydomain\.net$ [NC]
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|png|gif|bmp|php)$ [NC]
RewriteRule (.+) pages/list.php?country=%1 [L,QSA]

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]

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

Categories