Redirect with full URL with condition in htaccess - apache

I want to add redirection code for HTTPS in the .htacess file so it redirects all HTTP requests to HTTPS. My goal is to Archive:
redirect "http://berryman.com" to 'https://berryman.com"
redirect "http://berryman.com/mls" to
'https://berryman.com/mls"
Here is the code that I'm using for that
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
#Inorder to access object1
RewriteCond %{REQUEST_URI} !^/app
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
#Inorder to access object2
RewriteCond %{REQUEST_URI} !^/portal
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)berryman\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://berryman.com/$1 [R,L]
this works fine for the first condition, but I can't able to achieve the second condition. Every time I use berryman.com/mls it's add berryman.com/index.php behind it and shows 404 Page Not Found error.

Related

Adding directory to url with htaccess & rewrite

I have a site based on Modx CMS and I need to create a 301 redirect for the main folder & old links after the multilanguage functionality has been enabled.
What I need to achieve is the default fallback to english eg. make sure that:
example.com/products -> example.com/en/products
example.com/about - example.com/en/about
etc.
I also need to make sure that if there already is a language selection (for example de) in the url, I don't add en to url. (so no example.com/en/de/products)
I am having trouble adding the /en/ to url and I am ending up with infite /en/en/en loops on the URL
To add the /en/ to the url I tried the following.
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
It results to a endless /en/en/en loop in the url.
The whole htaccess is as follow:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Try the code below. The rules are chained (the default is AND). So, if your URL doesn't start with /en, /de, it's not the index (/), and it's not a file name or a directory, do a 301 redirect to the English version:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/en
RewriteCond %{REQUEST_URI} !^/de
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://%{HTTP_HOST}/en%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

htaccess Redirect all except the specified URL

Cannot use (!) reverse operation, a redirect URL (http://ssvwv.com) will appear:
/1/3/ktrb
Will not display (index.html)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^\/1\/(?:1|2|3)\/.*$
RewriteRule ^(.*)$ http://ssvwv.com/ [L,R=302,NE]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . /index.html [L]
This should work for you:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(?:index\.html|1/[1-3]/.*)$ [NC]
RewriteRule ^ http://ssvwv.com/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]
Issue is that last rule is rewriting to /index.html and when mod_rewrite loops again URI becomes /index.html and RewriteCond %{REQUEST_URI} !^\/1\/(?:1|2|3)\/.*$ returns true thus redirecting to http://ssvwv.com/. That's why you need to skip /index.html from first rule as well.

.htaccess https to http redirect conflict with existing rule

Currently we have this rule in .htaccess
RewriteEngine on <br>
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]
What this does is for any visit we will redirect to subfolder call site if the page not found. And when this happen the url will not show site as subfolder.
Example if we have
/rootFolder/site/temp.html this will show in url as
http://www.domain.com/temp.html
This is working fine but now we need to add https redirect if user visit site.
This is the new rule I came up with
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://%{HTTP_HOST}/site/$1
RewriteRule ^(/)?$ https://%{HTTP_HOST}/site/index [L]
But the issue with this one is now url will show site subfolder
https://www.domain.com/site/temp.html
How can I achieve so that if user does
http://www.domain.com/temp.html it will find temp.html in site subfolder and redirect to https and url will only show
https://www.domain.com/temp.html
Thanks
How about if you do it in two passes, always redirecting whatever was requested to https, then running your rewrite rules, like so:
RewriteCond %{HTTPS} off
#301 flag redirects instead of rewriting
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#leaving your original rules as is
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]

Apache Rewrite if file not found to another file?

I am trying to rewrite my urls to another file if they do not already exist. I have an .htaccess file in the root of my site that looks like this.
RewriteEngine on
# Add slash to end of request
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# API redirect
RewriteCond %{REQUEST_URI} ^/api
RewriteRule . api/v1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^ secondary\.
RewriteRule . sites/secondary [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sites/main.php [L]
My intention is to serve two sites from the same folder and use the rewrites to point to the appropriate routers. The api redirect works great. However, the main and secondary redirect do not work unless I specify a route other than example.com (example.com/test/ correctly rewrites).
Any insight is greatly appreciated.
Use:
RewriteEngine on
# Add slash to end of request
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule ^ %{REQUEST_URI}/ [R=301,L]
# API redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api api/v1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^ secondary\.
RewriteRule ^ sites/secondary [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ sites/main.php [L]
Because . is at least equal to one character.

htaccess redirect to subfolder with a trailing slash

I successfully managed to redirect all requests to a subfolder of my web server.
This is the code I'm using:
/.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1 [L]
/public/.htaccess
# redirect all urls that doesn’t have a
# trailing slash to urls with a trailing slash
# (this is my try)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1 [NC]
It works perfectly, so the following redirects work:
example.com -> example.com/public/
example.com/foo/ -> example.com/public/foo/
example.com/foo/about.html -> example.com/public/foo/about.html
By the way, the following redirect has a problem:
example.com/foo -> example.com/public/foo
Although it redirects correctly, the actual URL in the address bar is:
example.com/public/foo/
So my question is:
How can I have a clean URL when redirecting from a URL without a forward slash?
Hope it makes sense.
Place this code in root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1/ [L]
Then just remove /public/.htaccess since that is not needed.