Forward all requests but handle root call differently - .htaccess

I have a domain (e.g. doma.in) I am only using for forwarding purposes. So I created this .htaccess definition:
RewriteEngine On
RewriteRule ^(.*)$ http://www.mydomain.tld/in/$1 [R=301,L]
This successfully forwards calls in the following scheme
http://doma.in/abc -> http://www.mydomain.tld/in/abc
http://doma.in/123 -> http://www.mydomain.tld/in/123
http://doma.in/abc/123 -> http://www.mydomain.tld/in/abc/123
But I want to forward the root call, with or without www:
http://doma.in
http://www.doma.in
http://doma.in/
http://www.doma.in/
to the root URL of http://www.mydomain.tld.
I have tried to add another RewriteCond, but this setting throws a server error on all calls:
RewriteEngine On
RewriteRule ^(.*)$ http://www.mydomain.tld/o/$1 [R=301,L]
RewriteCond ^(.*)doma.in/?$
RewriteRule ^(.*)$ http://www.mydomain.tld [R=301,L]
How do I have to adapt my .htaccess in order to support my requirement?

You can use:
RewriteEngine On
# root URL
RewriteRule ^/?$ http://www.mydomain.tld/ [R=301,L]
# all but root URL
RewriteRule ^(.+)$ http://www.mydomain.tld/in/$1 [R=301,L]
Make sure to test this in a a new browser to avoid old 301 cache.

Related

Redirect http to https and www to non-www in .htaccess

First of all, I know there are lots of answers on this, but I don't actually find one that works. This is what I have in the .htaccess file right now, and I want to mention that it worked previously, but it does not anymore.
Redirect 301 /unt-de-cacao-de-plaja/filtre/producator/crisnatur/ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Require all denied
## For apache 2.2 and older, replace "Require all denied" with these two lines :
# Order deny,allow
# Deny from all
</FilesMatch>
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
# FORCE HTTPS AND NON WWW
RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
As a mention, I will have a lot of Redirect 301 from old pages to the new ones since the entire structure has been changed.
And the links that I am redirecting inside my website come with "www" like:
https://www.example.com/unt-de-cacao-de-plaja/filtre/producator/crisnatur/
and needs to be redirected to:
https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g
Redirect to https and non-www
To instead redirect all requests to https and non-www, use the following code instead of the previous:
Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]
</IfModule>
As before, place this code in the root .htaccess of your site. Here is what it's doing:
Checks if mod_rewrite is available
Checks if HTTPS is off, or if the request includes www
If either condition matches, the request qualifies and is redirected
to the https/non-www address
OR
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
A few issues, in order of importance:
You have your canonical HTTP to HTTPS and www to non-www redirects at the end of the file. By placing it at the end of the file, after your front-controller, it's simply never going to be processed for most requests. This needs to be near the start of the .htaccess file, before your front-controller.
You should avoid mixing redirects from both mod_alias (Redirect) and mod_rewrite (RewriteRule) in the same scope. Different modules execute at different times throughout the request, despite their apparent order in the config file. Since mod_rewrite is required for other redirects, you should convert the mod_alias Redirect directives to use RewriteRule instead.
For example:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ /ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
You should include the canonical scheme and hostname in your URL redirects in order to avoid multiple redirects when requesting an "old" URL at a non-canonical scheme ot hostname.
For example:
RewriteRule ^unt-de-cacao-de-plaja/filtre/producator/crisnatur/$ https://example.com/ingrijire-corporala/unt-cacao/unt-de-cacao-pentru-plaja-100g [R=301,L]
Depending on what you mean exactly by "a lot of Redirect 301" - you should not be doing this at all in .htaccess and instead redirecting in your server-side script, once you have determined that the request will 404. This is to prioritise normal site visiters and not your redirects (that get executed on every single request).
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Since you stated that these directives worked previously then I assume the use of the HTTPS environment variable is OK on your system. But note that, whilst this is relatively common, it's non-standard. (It implies the server is using some kind of SSL front-end/proxy.)
Note that the order of these rules will result in a double redirect when requesting http://www.example.com/<anything> (HTTP + www). Which is necessary if you are implementing HSTS, but otherwise, you should reverse these two rules to avoid this unnecessary double redirect.

How to hide .htaccess redirect from user

I have server with domain www.domain.com and multiple sub domains sub1.domain.com, sub2.domain.com. They are all pointing to server root.
I'd like users to access specific folders by subdomains. For example:
sub1.domain.com/someURI => sub1.domain.com/subFolder1/someURI
sub2.domain.com/someURI => sub1.domain.com/subFolder2/someURI
I would like to hide these redirections from users. I tried following .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1.domain.com$
RewriteRule ^(.*)$ http://sub1.domain.com/subFolder1/$1 [R,L]
RewriteCond %{HTTP_HOST} ^sub2.domain.com$
RewriteRule ^(.*)$ http://sub2.domain.com/subFolder2/$1 [R,L]
It is redirecting correctly only without any URI and redirection is visible.
You have 2 issues that are causing external redirect:
Using R flag in RewriteRule
Using Absolute URL starting with http:// in target
Another issue is that your rewrite rule is unconditional which can cause infinite looping.
You can use these rules:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} =sub1.domain.com
RewriteRule ^((?!subFolder1/).*)$ subFolder1/$1 [NC,L]
RewriteCond %{HTTP_HOST} =sub2.domain.com
RewriteRule ^((?!subFolder2/).*)$ subFolder2/$1 [NC,L]

How can I use .htaccess to stop redirecting from subdomains?

I have a domain, "domain.com", and subdomain, "sample.domain.com", and all files related to both are stored in domain.com/folder. How can I use .htaccess to prevent requests from "sample.domain.com" from going to domain.com/folder/index.php?title=sample?
This is what I'm currently using in my .htaccess file:
RewriteEngine on
RewriteCond %{http_host} .
RewriteCond %{http_host} !^www.domain.com [NC]
RewriteCond %{http_host} ^([^.]+)\.domain.com [NC]
RewriteRule ^(.*) http://www.domain.com /folder/index.php?title=%1 [R=301,L,QSA]
But there is one thing - a redirect is external (the browser goes to a new link), and I need to redirect this was on the server side, so that the user thought he was actually working with the subdomain. Can can I accomplish this?
Change this line:
RewriteRule ^(.*) http://www.domain.com /folder/index.php?title=%1 [R=301,L,QSA]
To
RewriteRule ^(.*) /folder/index.php?title=%1 [L,QSA]
The R flag tells the rewrite engine to redirect, and having the http://www.domain.com in the rule's target is also an implicit redirect.

SubDomain.DOMAIN.COM to remain as such in browser address bar - .htaccess / mod_rewrite

RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.com
RewriteCond %{HTTP_HOST} ^(.*)\.DOMAIN\.com
RewriteRule ^(.*)$ http://DOMAIN.com/FolderName/$1 [L]
If I type in SubDomain.DOMAIN.COM it redirects me to DOMAIN.COM/Folder/ fine, but I do not want the url in the browser address bar to change to DOMAIN.COM/Folder/ but remain as SubDomain.DOMAIN.COM.
Any clues to this.
RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.com
RewriteCond %{HTTP_HOST} ^(.*)\.DOMAIN\.com
RewriteRule ^(.*)$ http://DOMAIN.com/FolderName/$1 [L]
When a rewriterule points to a domain, an explicit redirect will occur. By default a 302 redirect (temporary redirect).
I suggest you to use the P(proxy) flag. For this to work, mod_proxy should be enabled.
RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.com
RewriteRule ^(.*)$ http://DOMAIN.com/FolderName$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(.*)\.DOMAIN\.com
RewriteRule ^(.*)$ http://DOMAIN.com/FolderName/$1 [L,P]
Also remember to set-up a ProxyReverse directive.
Context: server config, virtual host, directory
So, you cannot have a ProxyReverse in .htaccess.
Taken directly from: Proxying Content with mod_rewrite Apache Docs.
Consider using either ProxyPass or ProxyPassMatch whenever possible in preference to mod_rewrite.
Visit this for how to ProxyPass: https://stackoverflow.com/a/9189447/858515
Look at RewriteRule docs:
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the
hostname matches the current host. If it does, the scheme and hostname
are stripped out and the resulting path is treated as a URL-path.
Otherwise, an external redirect is performed for the given URL.
So - try to use direct filesystem path:
RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.com
RewriteCond %{HTTP_HOST} ^(.*)\.DOMAIN\.com
RewriteRule ^(.*)$ /path/to/domain.com/DocumentRoot/FolderName/$1 [L]

Redirect website root to subdirectory

How can I redirect all requests going to web root to another folder (e.g. public/)?
I've already tried this (contents of .htaccess in web root):
RewriteEngine on
RewriteRule ^(.*)$ public/$1
But now I have duplicate content for addresses:
address.tld/ and address.tld/public/
I would like to redirect address.tld/public/ to address.tld/, so there won't be any duplicates, but I just don't know how to do it and not get into redirecting cycle...
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /public/
RewriteRule ^public/(.*) /$1 [L,R=301]
RewriteRule !^public/ public%{REQUEST_URI} [L]
With mod_rewrite you'll not get redirecting cycles.

Resources