Redirect request using .htaccess - apache

I need to do the following Redirect using .htaccess
http://example.com => https://example.com/subdirectory
http://www.example.com => https://example.com/subdirectory
https://example.com => https://example.com/subdirectory
https://www.example.com => https://example.com/subdirectory
but i do not want to show sub directory in address bar the url should be https://example.com my site files lives in sub directory but i need to hide it in address bar for every request.

Try these rules:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
RewriteRule ^$ subdirectory [L]

How about this (building on Gumbo's answer):
RewriteEngine On
# Redirect direct subdirectory/ request to main domain
RewriteCond %{THE_REQUEST} ^(POST|GET)\s/subdirectory
RewriteRule ^subdirectory/?(.*)$ https://example.com/$1 [R=301,L]
# Redirect www/non-https to https://example.com
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^.*$ https://example.com/$0 [R=301,L]
# Redirect (internally) to subdirectory/
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule ^.*$ subdirectory/$0 [L]

Related

All http traffic to domain.com and all https to sub.domain.com with htaccess

I have two domain names www.domain.com and sub.domain.com, and I want to redirect all the http traffic to www.domain.com and all the https traffic to sub.domain.com. For example, if the url called is http:// sub.domain.com/index.php, the server have to rewrite the url to http:// www.domain.com/index.php, and if the url called is https:// www.domain.com/index.php, the server have to rewrite the url to https:// sub.domain.com.
I have this code in htaccess but it does not work correctly:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^/?$ "http\:\/\/sub\.domain\.com\/" [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]
It redirects correctly the https to sub.domain.com but only when there is no page indicated (https:// domain.com/index.php/some-page not work and https:// domain.com yes). For the http not work at all.
Thanks in advance!
You can put this code in your root htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Make sure both domain.com and sub.domain.com share the same document root folder. Otherwise, you will have to split htaccess code in two htaccess (one per root domain folder)
/domain/root/folder/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
/subdomain/root/folder/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

.htaccess redirect if domain = x.example.com

I have a site on a subdomain:
x.example.com
I put it on a subdomain because it's running on a separate server from the main site.
Using proxy for apache, I have now moved the site to example.com/x/ I want to make a 301 redirect from the subdomain like this:
Redirect 301 / http://example.com/x/
but I don't know how to make sure that it does not result in a redirect loop, as the page i'm redirecting to contains the same htaccess-file (as it's a proxy of the old file)
this is what I tried:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/ [R=301,L,NE]
this is my full .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^zenqa.herokuapp.com$ [NC]
RewriteRule ^(.*)$ http://staging.zenconomy.se/faq/$1 [R=301,L,NE]
</IfModule>
You're missing a backreference, you need a $1 to get the original request and pass it along to the redirect:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/$1 [R=301,L,NE]

url rewriting to redirect www to non www on https

I just changed my website http to https. I'll already redirected http to https version. I also want to redirect www version to non www.
So far couldn't succeed it.
http://www.domain.com => https://domain.com
https://www.domain.com => https://domain.com
Here is the .htaccess file;
<ifModule mod_rewrite.c>
RewriteEngine On
redirect 301 /makale /sozluk
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L]
RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>
Thank you.
You should remove this bit:
RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
And add this just under the RewriteEngine On:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.sporapp\.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
You need to have the redirecting happening before you rewrite, otherwise the request gets changed before it gets redirected.
Also, you have mod_alias directives which will interfere with the rewrite to /index.php:
redirect 301 /makale /sozluk
Should be:
RewriteRule ^makale/(.*)$ /sozluk/$1 [L,R=301]

WWW Resolve .htaccess Showing index.php in URL

So im using .htaccess to resolve all non-www requests to the www. domain, but the code im using produces a url which always shows index.php even from direct requests i.e. typing example.co.uk --> loads www.example.co.uk/index.php.
Heres the code:
RewriteCond %{HTTP_HOST} !^www\.site\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.site.co.uk/$1 [R=301,L]
Full .htaccess
RewriteEngine On
RewriteBase /
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} !^www\.site\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.site.co.uk/$1 [R=301,L]
AddHandler php55-script .php .php5
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#2 Permanent URL redirect
Redirect 301 /index.html http://site.co.uk/index.php
#3 Permanent URL redirect
Redirect 301 /lizards.html http://site.co.uk/stocklist/lizards.php
#4 Permanent URL redirect
Redirect 301 /snakes.html http://site.co.uk/stocklist/snakes.php
#5 Permanent URL redirect
Redirect 301 /amphibians.html http://site.co.uk/stocklist/amphibians.php
#6 Permanent URL redirect
Redirect 301 /misc.html http://site.co.uk/stocklist/misc.php
#7 Permanent URL redirect
Redirect 301 /tortoisesandturtles.html http://site.co.uk/stocklist/tortoisesandturtles.php
You need to make sure to keep this www rule before your other rules.
Alternatively you can modify this rule a bit:
# for index.php OR home
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(index\.php)?$ http://www.reptilesplus.co.uk/ [R=301,L]
# for rest
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.+)$ http://www.reptilesplus.co.uk/$1 [R=301,L]
Yes, that is possible by adding index\.php to a part of the first argument that isn't being copied over to the url. You can make it optional by using ?, which makes the previous character/group match either 0 or 1 times. We can use the [OR] flag to check if either the url doesn't start with www. or index.php is present in the url.
The following (untested) code should do the trick
RewriteCond %{HTTP_HOST} !^www\.reptilesplus\.co\.uk$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/index\.php
RewriteRule ^(index\.php/?)?(.*)$ http://www.reptilesplus.co.uk/$2 [R=301,L]
This should happen:
http://example.com/index.php/1 --> http://www.example.com/1
http://example.com/1 --> http://www.example.com/1
http://www.example.com/index.php/1 --> http://www.example.com/1
http://www.example.com/1 --> Doesn't match

.htaccess redirect without change url same host

I want to redirect a subdomain inside panel, the subdomain editor.domain.com
It should redirect to domain.com/cmseditor.
However the visitor should see editor.domain.com.
So far I've tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^editor\.homecatering\.pt$ [NC]
RewriteRule ^ http://homecatering.pt/cmseditor%{REQUEST_URI} [L,R=301]
Try something like this:
RewriteEngine On
# If the subdomain is "editor.homecatering.pt"
RewriteCond %{HTTP_HOST} ^editor.homecatering.pt$ [NC]
# Then rewrite any request to directory /cmseditor
RewriteRule ^((?!cmseditor).*)$ /cmseditor/$1 [NC,L]
or
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.homecatering\.pt$ [NC]
RewriteRule ^(.*)$ http://homecatering\.pt/cmseditor/%1