How to combine different apache redirects? - apache

I am trying to combine different apache redirects. I thought just getting rid of any Last flag would do the trick but it didn't (see How to combine Apache redirects?). It makes the following two landing page redirects
example.org -> www.example.org -> www.example.org/products/
But I'd rather have
example.org -> www.example.org/products/
to avoid delays.
Here is the .htaccess file:
# FORCE WWW
RewriteCond %{HTTP_HOST} !^www\.example\.org$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301]
# REDIRECT FROM ROOT TO PRODUCTS(.PHP)
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ /products/ [R]
RewriteRule ^products/$ /products/products.php
# ADD TRAILING SLASH
RewriteRule ^(([^/]+/)*[^./]+)$ http://www.example.org/$1/ [R=301]
# INTERNAL REWRITE OF "products/myname/myversion/" to "products/products.php?name=myname&version=myversion"
RewriteRule ^products/([a-z0-9-]+)/([0-9-]+)/$ products/products.php?name=$1&version=$2 [NC]
RewriteRule ^products/([a-z0-9-]+)/$ products/products.php?name=$1 [NC]

Try it like this currently I did not tried it,
#FORCE WWW with products
RewriteCond %{HTTP_HOST} ^example.org [OR]
RewriteCond %{REQUEST_URI} !^products
RewriteRule ^(.*)$ http://www.example.org/products/$1 [R=301]
And remove these lines
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ /products/ [R]

Related

Force www and redirect root directory

I couldn't find a question that answered mine, or maybe I couldn't search using the right terms, but come on. I have the following rule in my htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.br$
RewriteRule ^(.*)$ "https\:\/\/www\.domain\.com\.br\/site" [R=301,L]
This works when the user enters only the URL (domain.com.br or www.domain.com.br), but I need it to redirect when the user accesses this way:
domain.com.br or www.domain.com.br --> https://www.domain.com.br/site
domain.com.br/XXX --> https://www.domain.com.br/XXX
What rule should I use for this?
Update: the server already has a default rule to force SSL, in which case it is unnecessary to put it in htaccess
Rule update:
**On virtual host:**
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
**On htaccess file:**
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.br$
RewriteRule ^/?$ https://www.domain.com.br/site/ [R=301,L]
Your code generates a redirect loop. Also, you don't need to escape slashes on targets.
You could merge everything inside your virtual host block (faster than using a htaccess):
RewriteEngine On
# force https
RewriteCond %{HTTPS} off [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# redirect root directory to /site/
RewriteRule ^/?$ /site/ [R=301,L]
Of course, you will see a redirect chain for e.g. http://domain.tld/ as it will first redirect to https://domain.tld/ then to https://www.domain.tld/ and finally to https://www.domain/tld/site/. This is fine. However, if you really want to handle everything with only one redirect, you could. But, it will be less generic.
For instance, you would end up with those rules:
RewriteEngine On
# force root directory to /site/ (https+www)
RewriteRule ^/?$ https://www.domain.com.br/site/ [R=301,L]
# force any other pages to https+www if not the case already
RewriteCond %{HTTPS} off [NC,OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com.br%{REQUEST_URI} [R=301,L]

Getting .htaccess redirects from one domain to another to work for WWW ( both http://www. and https://www.), HTTP and HTTPS

I have a scenario for redirection I'm having trouble wrapping my head around.
The goals are:
old-domain.com/store needs to redirect to new-domain.com/store
There are three validation scripts that should remain on Old-Domain.com
Everything else from old-domain.com to redirect to new-domain.com
These rules need to apply to all scenarios HTTP, HTTPS and WWW (HTTP and HTTPS)
The problem I'm having is that the old-domain.com/store only properly redirects to new-domain.com/store when I remove the WWW redirects. I can't seem to get it to all play together nicely.
Here's what I've got:
Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ http://old-domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ https://old-domain.com/$1 [R=301]
RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html
Redirect 301 /store/ https://new-domain.com/store
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]
</IfModule>
We've also tried the following but then the validation scripts stop passing through:
Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ http://old-domain.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ http://old-domain.com/$1/ [R=301,L]
RewriteRule ^/?store https://new-domain.com/store [R=301,L]
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]
</IfModule>
The following rewrite rule will redirect all (www.)old-domain.com URLs to https://new-domain.com/ except URLs containing validation-script-(1|2|3).html
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/validation-script-(1|2|3)\.html$
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$
RewriteRule ^(.*)$ "https\:\/\/new-domain\.com\/$1" [R=301,L]
Check this demo https://htaccess.madewithlove.be?share=f0deda97-dff8-569e-bd6f-411b8a779361
Edit:
to redirect old-domain.com/store or old-domain.com/store/ to new-domain.com/pages/store
RewriteRule ^store/?$ "https\:\/\/new-domain\.com\/pages\/store" [R=301,L]

HTAccess Mod_Rewrite HTTPS WWW Keeping URL

I'm hoping someone here will be able to help me as I'm slowly losing my mind trying to get a rewrite rule working in HTAccess.
All I need is for 3 things to happen.
1) All www. traffic should go to non-www.
2) All non-https traffic should go to https.
3) All URLs should be preserved through these rewrites.
I've tried countless different examples online (and my own bastardised variations on them) without achieving the results I'm after. The site uses Joomla, so there's a fair amount of crap that Joomla includes as standard, but I'm pretty sure none of that is interfering with it.
With the current HTAccess the 'www.' is being dropped and the user is forced to https, but the URL is being lost.
Thanks in advance for your assistance!
My HTAccess file is here:
AddHandler application/x-httpd-php53 .php
ErrorDocument 401 "Authorisation Required"
##
# Joomla Crap START
##
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteBase /
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
##
# Joomla Crap END
##
# rewrite www.mywebsite.co.uk > mywebsite.co.uk
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# rewrite http > https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Redirect 301 /~fitspace https://mywebsite.co.uk
RedirectMatch 301 ^/index.php/(.*)$ https://mywebsite.co.uk/$1
Try :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]
The rule above will redirect
http://www.example.com/
to
https://example.com/
And
https://www.example.com/
to
https://example.com/

htaccess rewrite from a php file to subdomain

I would like to redirect from example.com/profile.php?UserName=xxxx to xxxx.example.com
xxxx contains a-z or 0-9.
I have tried bunch of codes to do it (some from this site) but none of them worked as I wanted it to be. Here is my current code:
RewriteEngine on
#this should redirect example.com/profile.php?UserName=x to x.site.com
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
#if link does not contain subdomain add www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
You need to put all of your redirect rules before your internal routing rules. The rules with the R flag redirect.
RewriteEngine on
# This redirects the browser to include the "www"
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# This redirects the browser when profile.php is directly accessed
RewriteCond %{THE_REQUEST} \ /+profile\.php\?UserName=([a-z0-9-_]+)
RewriteRule ^ http://%1.example.com/? [L,R]
# this internally routes the XXX.example.com to the profile.php script
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule ^$ /profile.php?UserName=%1 [QSA,L]
Since there was not a good answer here.. In order to rename a php file and use the subdomain extention.. I had to edit my DNS settings.. This tutorial helped :http://www.mediacollege.com/internet/server/apache/mod-rewrite/subdomains.html

Properly redirecting a subdomain to subdirectory invisibly

I know similar things have been asked before, but none of the solutions I've found have seemed to work out. I'm by far an expert when it comes to mod_rewrite and it's ilk, so apologies if I'm missing something obvious.
I am trying to invisibly redirect subdomains to an index.php file in a subdirectory; this file takes the value of the subdomain as part of the query string, which is working fine.
The problem I'm having is that now everything in this subdirectory is being redirected to the index.php file, which I don't want to happen.
This is what I have thus far:
RewriteEngine On
RewriteBase /
# User dashboards
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,NC,QSA]
What I'm looking for is a situation where http://subdomain.example.com/ will lead to /app/index.php?user=subdomain, but http://subdomain.example.com/assets/stylesheet.css will go to /app/assets/stylesheet.css.
Thanks in advance!
If i understood well your example, you can do it this way:
redirects example.com to www.example.com to avoid having "empty" subdomain
internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
internally rewrites other things with "app" prefix
Which is represented by this code
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
EDIT: as you asked in below comments, here is how to manage also www subdomain
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally redirects www subdomain root to /site/index.php
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?$ /site/index.php [L]
# internally rewrites every other root subdomains to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
Let add second rule to redirect assets to app/assets:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif)$ [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
or load all css/js/images directly from app:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*\.(css|js|png|jpg|gif)$ app/$0 [NC, QSA]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
EDIT: Sorry, I haven't tested it before, so there is working example:
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule !^app/assets/ app/index.php?user=%1 [L,QSA]