.htaccess: How can I keep the redirection and remove the subdirectory path from the urls please? - apache

I am trying to direct traffic to a subdirectory using .htaccess
The subdirectory is: /Release2/code.
The following is successfully redirecting but its is not hiding /Release2/code from the urls
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$
RewriteCond %{REQUEST_URI} !^/Release2/code
RewriteRule ^(.*)$ /Release2/code/$1 [L]

Related

.htaccess rewrite in subdirectory

I am currently deploying a number of sites from one hosting account. I have all of the sites in their own folder including the primary domain. The issue I have is when I rewrite the primary domains address with my current code, it includes the subdirectory in it. So currently if I type in http://www.example.com/url it rewrites to https://example.com/folder/url. I just want it to rewrite without the folder.
Any ideas. I know I am complicating this by running my primary domain in a subdirectory, just trying to clean up hosting as best as possible.
In my public_html .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ folder/index.php [L]
and in public_html/folder .htaccess:
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
So currently if I type in http://www.site.whatever/url it rewrites to https://site.whatever/folder/url.
This is a "redirect", not a rewrite.
This is happening because of the use of the REQUEST_URI server variable in your HTTP to HTTPS redirect in your public_html/folder .htaccess file:
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
The REQUEST_URI server variable contains the full URL-path of the request, which, by the time the subdirectory's .htaccess file is called, has been updated to contain /folder.
You need to either:
Move your canonical www to non-www and HTTP to HTTPS redirects to the .htaccess file in the document root. (This would be preferable if you have no other mod_rewrite directives in your public_html/folder .htaccess file.)
OR,
Modify the above directive to use the $1 backreference (to the captured RewriteRule pattern) as you are doing in the preceding www to non-www redirect. For example:
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
(Note that this should ultimately be a 301 redirect, once you have confirmed it works OK.)
And don't forget to escape literal dots in the regex.

htaccess redirect with question mark

Hello I'm trying to redirect /fr/welterweight.php which is a local folder on my website via apache conf or .htaccess.
To http://www.bluek.com/fr/welterweight.php?c=france which is another website I own.
I tried
RewriteCond %{REQUEST_URI} /fr/welterweight.php
RewriteCond %{QUERY_STRING} ^welterweight.php$
RewriteRule ^/?$ http://www.bluek.com/fr/welterweight.php?c=france
But its not working
This rule should work from site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^fr/welterweight\.php$ http://www.bluek.com/$0?c=france [L,R=302]

.htaccess let subdomain behave as main domain

I would like to create subdomain just for one page of main domain. I don't want to have copy of files so i try to solve it using mod_rewrite.
Simply when i go to submdomain.domain.com i want to show page xyz.html as index.html with all pictures. The mod rewrite should keep address in browser url as subdomain.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.domain.com
RewriteCond %{REQUEST_URI} !(index.html)
RewriteRule ^(.*) http://www.domain.com/$1 [P,L]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com
RewriteCond %{REQUEST_URI} index.html
RewriteRule index.html http://www.domain.com/xyz.html [P,L]
this .htaccess is saved in subdomain of root directory. Unfortunately it works only separatly. First half works only when second half is missing and vice versa. So i am able to do redirect to show only xyz.html page as index page but without images, or do redirect for all subdomain files but xyz.html is ignored and main domain index.html is used.
at the end i have found the solution
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.domain.com
RewriteCond %{REQUEST_URI} index.html
RewriteRule index.html http://www.domain.com/myspecial-page.html [P,L]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com
RewriteRule ^([a-zA-Z0-9\.\-\_]+).html$ http://www.domain.com/$1.html [NC]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com
RewriteRule ^images/([a-zA-Z0-9\.\-\_]+)$ http://www.domain.com/images/$1 [NC,P]
etc...for css, js
it is possible to test rules using htaccess tester

redirect subdomain to folder without changing url

I want to redirect the www.domain.com to the www folder without changing the url in the address bar (internal redirect), I tried and used the following code but it's not working
I do this to organize my public_html folder
the .htaccess file is in the public_html folder
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www)\.domain\.com
RewriteRule ^(.*)$ /www/$1 [L]
Try this rule
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www)\.domain\.com$ [NC]
RewriteRule !^www/ /www%{REQUEST_URI} [L,NC]

redirect into subdirectory AND out of subdirectory

i have a mod_rewrite redirection problem i cannot figure out.
all requests from a specific domain get "silently" rewritten into a designated subdirectory. e.g. www.mydomain.net/hello.html retrieves the file in /net/hello.html. the following .htaccess (placed in my hosting root) achieves this perfectly:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200 # <-- i will need this later. read to the end of the post.
RewriteRule .* - [L]
rewriteCond %{HTTP_HOST} ^www.mydomain.net$
rewriteCond %{REQUEST_URI} !^/net.*$
rewriteRule (.*) /net/$1 [L]
however, direct URLs into this directory however should visibly redirect with a 301 to the URL without that subdirectory. e.g. www.mydomain.net/net/hello.html should redirect to www.mydomain.net/hello.html (which than still retrieves the file in /net/hello.html). my .htacces file for this (placed in /net) unfortunately doesn't work:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^(.*) /$1 [R=301,L]
i get an infinitive redirect loop despite the RewriteCond %{ENV:REDIRECT_STATUS} 200 block in the root .htaccess file... so what's wrong?
btw, i have to use mod_rewrite, because the site is externaly hosted and i have no access to the apache configs.
many thanks for any pointers.
Inspect the HTTP request line in THE_REQUEST instead:
RewriteCond %{THE_REQUEST} ^GET\ /net[/? ]
RewriteRule ^net($|/(.*)) /$2 [L,R=301]