I understand the basics of .htaccess rewriting, but the following confuses me a bit...
What I want is to rewrite a subdomain 'sub.domain.com' in a way that it looks for the files of 'seconddomain.com/folder/'. Without a simple 301 redirect, I want to keep showing the URL 'sub.domain.com'.
So 'sub.domain.com/file.html' should get the content of 'seconddomain.com/folder/file.html'.
Who can help me with the right htaccess-rules? Thanks in advance.
(I assume this can be accomplished the easiest with .htaccess rewriting, but if you have a better solution to do the same, let me know!)
Well this is what I tried, but it only redirects and adds an 'public_html' into the redirected URL (so the subdomain is 'mijn.pingweb.nl' and the folder to redirect is 'factuur.pingweb.nl/Klanten/':
RewriteEngine on
Options +FollowSymlinks -MultiViews
# handles http redirect sub-dir
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^(.+\.)?mijn\.pingweb\.nl$ [NC]
RewriteRule ^/?(.*)$ http://factuur.pingweb.nl/Klanten/feed?v=$1 [R=301,L,QSA,NE]
# handles http redirect non sub-dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^(.+\.)?mijn\.pingweb\.nl$ [NC]
RewriteRule ^/?(.*)$ http://factuur.pingweb.nl/Klanten/$1 [R=301,L,QSA,NE]
# handles https redirect sub-dir
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^(.+\.)?mijn\.pingweb\.nl$ [NC]
RewriteRule ^/?(.*)$ https://factuur.pingweb.nl/Klanten/feed?v=$1 [R=301,L,QSA,NE]
# handles https redirect non sub-dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^(.+\.)?mijn\.pingweb\.nl$ [NC]
RewriteRule ^/?(.*)$ https://factuur.pingweb.nl/Klanten/$1 [R=301,L,QSA,NE]
Related
I have a main domain and many subdomains on it now i want to redirect main domain to its HTTPS and other domains to its subdomain for example:
main.com redirects to https://main.com
domain1.com redirects to domain1.main.com
domain2.com redirects to domain2.main.com
now i using this
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^www.main.com [OR]
RewriteCond %{HTTP_HOST} ^main.com [NC]
RewriteRule ^(.*)$ https://www.main.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
this code redirect main.com only and other domains must check in index.php and redirect to specific subdomain, i want to perform whole work in htaccess.
This probably is what you are looking for:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?main\.com
RewriteRule ^ https://www.main.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^(?:www\.)?main\.com
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.com
RewriteRule ^ https://%1.main.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
It makes sense to start out using a R=302 temporary redirection at first and only change that to a R=301 permanent redirection once everything works as desired.
Obviously this requires that requests to those other domains are handled by the http host which implements this rule set. So you most likely want to define either host aliases for those domains or use a default virtual host inside your http server.
First I directed my /example folder to main domain. Now, I'd like to redirect some of the pages to HTTPS. Pls, anyone could advice me about the code:
# com upload yonlendirme
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/example/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ example/index.php [L]
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#redirect www.mydomain.com to mydomain.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(index\.php?route=account/register|index\.php?route=account/login|index\.php?route=account/account|index\.php?route=checkout/checkout)$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You can't match against the query string (everything after the ?) in a rewrite rule. You'll need to match against the %{QUERY_STRING} variable in a rewrite condition:
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} ^route=(account/register|account/login|account/account|checkout/checkout)
RewriteRule ^index.php$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You may also want to move that rule up to the top of your htaccess file so the routing to the example folder doesn't interfere with the redirects.
I'm using .htaccess to force https on certain pages and http on other pages and it's working fine. But I need to force http on home page (example: http://website.com) and I don't know how to do that. I tried RewriteCond %{REQUEST_URI} ^/index.php/? but as I'm using drupal that didn't work.
This is the script I'm using:
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/register/? [OR]
RewriteCond %{REQUEST_URI} ^/admin/?
RewriteRule ^(.*)$ https://website.com/$1 [R,L]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} ^/about-us/? [OR]
RewriteCond %{REQUEST_URI} ^/help/?
RewriteRule ^(.*)$ http://website.com/$1 [R,L]
Any help is appreciated
First, instead of RewriteCond %{SERVER_PORT}, you can use RewriteCond %{HTTPS}, which should be more reliable in detecting HTTPS.
The homepage has the RewriteRule pattern ^$. So the rule would be
RewriteCond %{HTTPS} =on
RewriteRule ^$ http://website.com [R,L]
When everything works as you expect, you can change R to R=301.
I'm redirecting sub domains (with an htaccess in each sub domain) to a new domain but I want to redirect the index page to a sub-directory on the new domain.
subdomain.example.com/index.php to example.com/folder/index.php
Thats for the Index page only for the rest of the subdomain
subdomain.example.com to example.com
Thats for all the content pages and Not the Index page.
I can't get both to redirect at same time. Is there a way ?
Try to put these rules in your .htaccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# handles http redirect sub-dir
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC]
RewriteRule ^/?(.*)$ http://www.anotheriste.com/feed?v=$1 [R=301,L,QSA,NE]
# handles http redirect non sub-dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC]
RewriteRule ^/?(.*)$ http://www.anotheriste.com/$1 [R=301,L,QSA,NE]
# handles https redirect sub-dir
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC]
RewriteRule ^/?(.*)$ https://www.anotheriste.com/feed?v=$1 [R=301,L,QSA,NE]
# handles https redirect non sub-dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC]
RewriteRule ^/?(.*)$ https://www.anotheriste.com/$1 [R=301,L,QSA,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters
$1 is your REQUEST_URI
Im trying to use htaccess to redirect any subdomain and the root of one domain to another domain.
example
anysub.mysite.com or mysite.com redirects to www.anotheriste.com
and also mysite.com/stuffhere to www.anothersite.com/feed?v=stuffhere
So anything that is not a subdirectory gets redirected to the other domain and anything that is a subdirectory gets redirected to the URL with the query string. Can't seem to get it. One rewrite rules overwrites the other.
Edit:
This is what I got to work
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^$ http://www.site.com [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/*\/?
RewriteRule ^(.*)$ http://www.site.com/feed?v=$1 [R=301,L]
Try putting these rules in your .htacccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# handles http redirect sub-dir
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC]
RewriteRule ^/?(.*)$ http://www.anotheriste.com/feed?v=$1 [R=301,L,QSA,NE]
# handles http redirect non sub-dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} =80
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC]
RewriteRule ^/?(.*)$ http://www.anotheriste.com/$1 [R=301,L,QSA,NE]
# handles https redirect sub-dir
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC]
RewriteRule ^/?(.*)$ https://www.anotheriste.com/feed?v=$1 [R=301,L,QSA,NE]
# handles https redirect non sub-dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} =443
RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC]
RewriteRule ^/?(.*)$ https://www.anotheriste.com/$1 [R=301,L,QSA,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA will append your existing query parameters
$1 is your REQUEST_URI