Htaccess Redirection with REQUEST_URI - domain folders added to URL - .htaccess

On our shared hosting server, we had a request to keep client's domain on their server but redirect the DNS responsible for web traffic to our server.
On our server we had to add an entry in the .htaccess file in our root to point it to a folder in the server:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.pl$ [NC]
RewriteCond %{REQUEST_FILENAME} !/WebsitesLive/Example/
RewriteRule ^(.*)$ /WebsitesLive/Example/$1 [L]
And the website works fine but we noticed in Google Analytics that some people access the website using https://example.pl/WebsitesLive/Example. I finally realised that (maybe) it's the HTTPS and non-www redirection in the htaccess file of the client's site:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Is it true that %{REQUEST_URI} would, in this case, contain WebsitesLive/Example in the redirection URL?
Most importantly, how do I stop it?

Is it true that %{REQUEST_URI} would, in this case, contain WebsitesLive/Example in the redirection URL?
Yes, after the internal rewrite from the root the REQUEST_URI server variable is updated to include the full URL-path. You could instead capture the URL-path in the RewriteRule pattern and use the backreference which will be relative to the directory that contains the .htaccess file.
For example:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
(You were already capturing the URL-path, but not using it.)
Ordinarily, if this canonical redirect was used in a subdirectory and that subdirectory was part of the visible URL then this would be incorrect, since it would remove the subdirectory from the redirected URL.
Alternatively, you could implement the canonical redirects in the root .htaccess file instead.
However, whilst this should prevent the filesystem directory being exposed in the canonical redirect, this doesn't prevent a user from accessing this subdirectory (from any domain). And since this subdirectory has already been exposed (especially as a 301 permanent redirect) then direct access to this subdirectory needs to be blocked or redirected back to the root. However, we need to be careful of redirect loops when doing so.
You can use something like the following in the subdirectory .htaccess to redirect any "direct" requests from the user back to the root:
# /WebsitesLive/Example/.htaccess
# Redirect direct requests to subdirectory back to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
The REDIRECT_STATUS environment variable is empty on direct requests, but set to the HTTP status code after the first internal rewrite - thus preventing internal rewrites to the subdirectory being redirected back to the root (an endless redirect loop).
Aside:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.pl$ [NC]
RewriteCond %{REQUEST_FILENAME} !/WebsitesLive/Example/
RewriteRule ^(.*)$ /WebsitesLive/Example/$1 [L]
Whilst this might "work", the second condition is too broad since it is checking that /WebsitesLive/Example/ does not occur anywhere in the "filesystem path". Whereas you should be checking that /WebsitesLive/Example/ does not occur at the "start" of the URL-path. In other words, it should be like this:
:
RewriteCond %{REQUEST_URI} !^/WebsitesLive/Example/
:
Note that this condition is only necessary (to prevent a rewrite loop) if there is no .htaccess file in the subdirectory (being rewritten to) that contains mod_rewrite directives. (Since mod_rewrite directives in the subdirectory completely override the parent - by default.)
If there is no .htaccess file in the subdirectory then you would obviously need to prevent direct access to that subdirectory in the root .htaccess file, but the required rule would be slightly different to the above. For example:
# /.htaccess
# Redirect direct requests to subdirectory back to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^WebsitesLive/Example/(.*) https://%{HTTP_HOST}/$1 [R=301,L]
# Rewrite to subdirectory
:

Related

How to rewrite a subdomain in htaccess

Im trying to simplify the subdomain complexity perception to the user so I would like to mask the subdomain.
My goal is the following:
When the user tries to get here
blog.example.com/blog/whatever
I would like to mask the subdomain to be
example.com/blog/whatever
Same content served but with the subdomain hidden.
I have tried the following but with no success.
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^https://blog\.example.com\/blog [NC]
RewriteRule ^(.*)$ http://example.com/blog/$1 [R=301,L]
</IfModule>
Update: Rewrite module is enabled
RewriteCond %{HTTP_HOST} ^https://blog\.example.com\/blog [NC]
RewriteRule ^(.*)$ http://example.com/blog/$1 [R=301,L]
The HTTP Host header (ie. the value of the HTTP_HOST server variable) contains the hostname only, ie. blog.example.com. It does not contain the full absolute URL.
So, your rule should be like this instead:
RewriteCond %{HTTP_HOST} ^blog\.example\.com [NC]
RewriteRule ^blog($|/) https://example.com%{REQUEST_URI} [R=301,L]
This matches the URL-path /blog or /blog/<whatever> (but not /blog<something>) and redirects to the same URL-path at example.com (+ HTTPS). The REQUEST_URI server variable contains the root-relative URL-path, starting with a slash.
You were redirecting to HTTP, but I assume this must be HTTPS?
You should clear your browser cache before testing and test first with a 302 (temporary) redirect to avoid potential caching issues.

how change url of this page on .htacess

So i have to change a url in .htaccess for SEO purposes, i already have a .htaccess file on the home directory of the site...
do i need to make the changes on the .htaccess on the home directory or on the subdirectory that is the web page that i need to have a new URL???
This is the code i already have on the home directory
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect /public_html/cursos/YoutuberKids/index.php /public_html/cursos-
kids/Youtuber/index.php
and this is the actual URL of the page that must be changed:
https://www.viahavok.com.br/cursos/GameDesignPRO/
And i need to become this URL:
https://www.viahavok.com.br/cursos/game-design-pro
Why don't you just change the name of the folder to 'game-design-pro'? Changing the URL in .htaccess isn't better or more effective when it comes to SEO in this case. (Unless I misunderstood your question?)
Add this to the bottom of the .htaccess file in the home directory:
RewriteRule ^cursos/GameDesignPRO/(.*)$ /cursos/game-design-pro/$1 [R=301,NC,L]
This will redirect all resources in the directory.
The R=301 indicates that this is a permanent move for the
resources.
The [L] flag causes mod_rewrite to stop processing the rule set.
In most contexts, this means that if the rule matches, no further
rules will be processed.
The [NC] flag causes the RewriteRule to be matched in a
case-insensitive manner.

Redirect Multiple URL within folder to other domain

I have some files placed under a particular folder in my old domain:
http://www.olddmain.com/subfolder/example1.html
I want to redirect all files under this folder to a new domain.
Example:
http://www.newdomain.com/subfolder/example1.html
How do I do this without losing Ranking of the pages?
Maybe you need this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
from "Redirecting to a different domain" in Redirecting a Web Folder Directory to another Directory in htaccess
In the olddomain.com/subfolder/.htaccess file you just need a single mod_alias Redirect directive to redirect all files to newdomain.com/subfolder/<file>:
Redirect 302 /subfolder http://www.newdomain.com/subfolder
Change the 302 (temporary) to 301 (permanent) only when you are sure it's working OK. (301s are cached hard by the browser, so can make testing problematic.)
However, if olddomain.com and newdomain.com point to the same place then you will need to use mod_rewrite and include a condition that specifically checks for the host being requested, otherwise you'll get a redirect loop. For example, in the same olddomain.com/subfolder/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [R=302,L]
This redirects any request for olddomain.com/subfolder/<anything> to http://www.newdomain.com/subfolder/<anything>.
Again, change 302 to 301 when you are sure it's working OK.

.htaccess exclude directories from a https: rewrite

I have my root directory where my main website is contained. On this site every request coming in I want to make sure it is over https: I use the following mod rewrite for this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.treacyshotelwaterford.com%{REQUEST_URI} [L,R=301]
My problem is that I have two sub directories which I run two other wordpress websites with different domains. Now since I introduced the mod rewrite in the .htaccess in the root all requests to the other domains get redirected to the main website. I have tried to exclude the folders but this has not effect.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(spiritbeautyspa.ie|spiritleisurecentre.ie) https://www.treacyshotelwaterford.com%{REQUEST_URI} [L,R=301]
I suppose those different domains are only pointing to those sub-directories, but do not have those in the actual URL path?
Then you can not check for them with a RewriteRule (because RewriteRule looks at the path component of the request URL only) – use a RewriteCond that checks the host name of the request instead.

force ssl with .htaccess while rewriting addresses

I am currently using this .htaccess to redirect all the requests for pages with a directory to my index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|cas)
RewriteRule ^(.*)$ /seattle/index.php/$1 [L]
And this works just fine and produces urls that hide the index.php, and I have code in index.php that makes urls clean looking.
But now I need to force pages to connect via ssl, so I tried
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond $1 !^(index\.php|cas)
RewriteRule ^(.*)$ https://example.com/seattle/index.php/$1 [L]
and it works to force ssl, but now it also forces the url to include the index.php:
https://example.com/seattle/index.php/pagename
instead of what I want
https://example.com/seattle/pagename
What am I missing?
To change protocol (HTTP -> HTTPS) and/or domain name (www.example.com -> example.com) the proper redirect ("301 Permanent Redirect" or "302 Found/Temp Redirect") is required.
Therefore you cannot combine rewrite and redirect and still showing original URL. It has to be 2 different rules and the one for changing protocol/domain should be listed first. For example:
RewriteEngine on
# force HTTPS for all URLs
RewriteCond %{HTTPS} =off
RewriteRule . https://example.com%{REQUEST_URI} [R=301,L]
# other rewrite rules
RewriteCond $1 !^(index\.php|cas)
RewriteRule ^(.*)$ /seattle/index.php/$1 [L]
The rule I have added will redirect ALL HTTP URLs to HTTPS. If you need only some of them to be redirected -- add appropriate conditions via additional RewriteCond line(s).
The %{HTTPS} is the most common and kind of "proper" way of checking if SSL is ON or OFF (but it is all depending on your specific circumstances and server config). When checking against %{HTTPS} you are safe against situation when your site is running on non-standard port (other than 80). You can use %{SERVER_PORT} =80 instead (will work for majority of cases).
With the above rules the rewrite for http://example.com/seattle/pagename will occur in 2 steps:
301 Redirect to https://example.com/seattle/pagename
Rewrite (internal redirect) to /seattle/index.php/pagename

Resources