Rewrite Rule in Apache/htaccess not working - apache

I'm trying to redirect user request to my domain in .htaccess file.
My requirement is for example if user request for example.co/abc it should redirect to www.example.co/abc.
Below is my rewrite rule.
RewriteCond %{HTTP_HOST} ^example.co/Abc [NC]
RewriteRule ^(/Abc)$ https://www.example.co/$1 [L,R=301]
After adding the above two lines in my .htaccess file it is redirecting from example.co to www.example.co
I tried in different ways still not getting where i'm doing mistake.
Any help would be appreciated.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ http://www.example.co/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{www.example.co} [L,R=301]
RewriteCond %{HTTP_HOST} ^example.co [NC]
#RewriteRule ^(/*)$ https://www.example.co/$1 [L,R=301]
RewriteRule ^/?(Abc)/?$ https://www.example.co/$1 [L,R=301]

Related

Forcing https on the whole site but not on one folder

Could anyone tell me how I can force https on my whole website but not on a single folder or url.
At the moment I have this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^thatmysite\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://thatmysite.com/$1 [R,L]
But if I add this second code in the root htaccess to remove https from the /thatsite.com/printing folder, I get a redirect loop because I am forcing on the code http to https and not https to http...
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(printing)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(printing)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
Do you know a way around this please? I have been looking all over the internet and cannot find a single good answer.
Try these 2 rules at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/printing [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /printing [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I solved this issue by placing this code in an .htaccess file the root folder of the directory that hosts the http site (e.g. public_html)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/TheFolderYouCanAccessWithoutHttp/
RewriteRule (.*) https://yourdomain.xyz/$1 [R=301,L]

How To exclude a Folder from 301 Redirect .htaccess

I have a 301 redirect on the domain goodereader.com that points to goodereader.com/blog/
I wanted to setup a forum, and wanted to exclude /forum/ from being 301'd
Here my current .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{HTTP_HOST} ^www\.goodereader\.com$ [NC]
RewriteRule ^(.*)$ http://goodereader.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^goodereader\.com$
RewriteRule (.*) http://goodereader.com/blog/$1 [R=301,L]
RewriteRule ^$ blog [L]
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
From the comments, you can fix this by adding the same condition to the 2nd rule. Rewrite conditions only get applied to the immediately following rule, they're not global in any way.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{HTTP_HOST} ^www\.goodereader\.com$ [NC]
RewriteRule ^(.*)$ http://goodereader.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{HTTP_HOST} ^goodereader\.com$
RewriteRule (.*) http://goodereader.com/blog/$1 [R=301,L]
RewriteRule ^$ blog [L]
or use a pass through:
RewriteEngine On
RewriteRule ^forum/ - [L]
RewriteCond %{HTTP_HOST} ^www\.goodereader\.com$ [NC]
RewriteRule ^(.*)$ http://goodereader.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^goodereader\.com$
RewriteRule (.*) http://goodereader.com/blog/$1 [R=301,L]
RewriteRule ^$ blog [L]
Make sure to clear your browser's cache. 301 redirects are permanent and the browser will always cache them.

Apache detected external loop redirection with target URL after .htaccess setup

I've played around with this for hours, trying to get it right. It seems to be working the way I want, but I'm getting this Apache error.
I want to set it up so if someone visits my main URL they're directed to https and www, so I have this in the htaccess file in the main public_html directory.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Next I want anyone who hits subdomain.domain.com or domain.com/subdomainname to redirect to https://subdomain.domain.com, so I have this in the .htaccess file in the subdomain folder.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.subdomain\.domain\.com$
RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]
Any hints as to what I'm doing wrong?
Replace your subdomain .htaccess by this:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !=subdomain.domain.com
RewriteRule ^(.*)$ https://subdomain.domain.com/$1 [R=302,NE,L]

redirect subdirectory to subdomain issue

I have .htaccess file like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteRule ^/?market/(.*)$ http://market.example.com/$1 [L,R=301]
RewriteRule ^/?buddies/(.*)$ http://buddies.example.com/$1 [L,R=301]
RewriteRule ^/?bazaar/(.*)$ http://bazaar.example.com/$1 [L,R=301]
It works fine in market subdirectory. It redirects to the subdomain. But, There is a problem with other 2 subdirectories.
ERROR FOR OTHER 2 sub-domains:
The page isn't redirecting properly
What should I do to overcome this problem?
It is because RewriteCond is only applicable to the very next RewriteRule. Your last 2 rules execute for all the hosts including buddies and bazaar and causes redirection loop.
You need these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteRule ^/?market/(.*)$ http://market.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteRule ^/?buddies/(.*)$ http://buddies.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteRule ^/?bazaar/(.*)$ http://bazaar.example.com/$1 [L,R=301]

htaccess rewrite rule fails on https redirection

my htaccess look like this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^localhost$
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^login/?(.*)$ login.php$1 [L]
RewriteRule ^register/?(.*)$ register.php$1 [L]
i want to redirect my pages login and redirect to https and so i added the code below
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]
so when i type http://www.xyz.com/login/ it redirects to https://www.xyz.com/login/ everything was fine when i decided to redirect the pages to http which are not login or register and i wrote
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]
after that i testted the url http://www.xyz.com/login/ again and it redirects me to https://www.xyz.com/login.php
any idea to redirect http://www.xyz.com/login/ to https://www.xyz.com/login/ and on that page when i click the link https://www.xyz.com/news/1/ redirect to http://www.xyz.com/news/1/
Try adding this right below the RewriteEngine On line:
RewriteCond %{HTTPS} off
RewriteRule ^(login|register)(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule !^(login|register) http://%{HTTP_HOST}%{REQUEST_URI} [L,R]
It also looks like you've pasted your rules wrong, the two blocks of rules are identical.